1

I'm trying to include Spring for Android as a client to a Spring server in a mobile app project. After looking at the documentation (https://docs.spring.io/spring-android/docs/1.0.1.RELEASE/reference/html/rest-template.html) I would like to use the method getForObject() to directly create the objects I will use in the code. All the exemples I find on internet only show what to do with more primitive type for the value of each key (String, long, int). The JSON object sent back from the server looks like this :

{
  "id": "MSP-SS-043208",
  "nom": "Inondation-Débit-Niveau",
  "source": "Ministère de la Sécurité publique du Québec",
  "territoire": "Rivière des Mille Îles(043208)",
  "certitude": "Observé",
  "severite": "Mineure",
  "dateDeMiseAJour": "lundi 04 juin 2018",
  "urgence": "Future",
  "description": "Seuil de surveillance atteint",
  "count": 1,
  "geometry": {
      "type": "Point",
      "coordinates": [-73.6387202781213, 45.6928705203507]
  },
  "type": "Suivi des cours d'eau"
}

My problem is I have no idea what to do with the geometry key because it is not a primitive object ! How can I make Spring recognize that there is a JSON object as the attribute of a certain key ? And what about the array of double (coordinates)?

This is the class I am trying to use:

public class Alert {

  private String id;
  private String nom;
  private String source;
  private String territoire;
  private String certitude;
  private String severite;
  private String dateDeMiseAJour;
  private String urgence;
  private String description;
  private int count;
  private ????? geometry;
  private String type;

  ... gettters and setters ...

}

My question comes down to : How must I declare the geometry attribute to make sure the objects are created correctly ?

1 Answer 1

1

You need to make a Geometry class that has some deserialization logic, and then declare private Geometry geometry as a member of the Alert class.

This deserialization is usually performed "automagically" by Spring (specifically, by the fasterxml library provided by jackson). Since the Geometry class looks like it has some nontrivial objects, you'll also need to define a Coordinates class and 'Type' class (or Enum) that can similarly be deserialized.

Sometimes this approach can create a lot of small classes that take a lot of work to deal with. Another approach is to use the @JsonCreator annotation on a constructor of the Alert class:

@JsonCreator
public Alert(Map<String, Object> params) {...}

Then you can parse the Alert json manually.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks a lot ! The type of "type" will always be a String so that is not a problem. Will Spring also automagically convert the coordinates attributes to a Java array ? (I think we can manage in our app to use only double arrays).
@OlivierL.Applin I believe Spring will convert a JSONArray to a List, I'm not so sure about an array (arrays aren't proper Objects so I'm not sure). And the Type I was referring to was inside of Geometry, but it's fine to use String for that I suppose.
Great ! Unfortunatly, it seems I can't send a request to our server right now, so I can't test ... but I'll keep you posted if it's working :) ! Thanks again
I can now properly do some request to our server and everything works fine except my infamous Geometry. What exactly do you mean by "deserialization logic" ? Spring client is all quite new to me
@OlivierL.Applin By deserialization logic I just mean logic that converts the raw JSON into a Java Object. This logic does not always have to be complex, but sometimes there are cases where the Java object is substantially different from the JSON, even though you can convert between the two.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.