-2

Say that I have the following Java class

public class ResponseObj
{
  private String a;
  private String b;

  //constructors, getters, and setters
}

Next I do a REST call to an API

ResponseEntity<ResponseObj> response = restTemplate.exchange("http://api.com/employee/24",
                  HttpMethod.GET, httpEntity, ResponseObj.class);

and the JSON response from the API is as follows

{
  "a" : "data1",
  "c" : "data2",
  "d" : { "prop" : "data3"},
  "e" : ["data4","data5"]
}

Will the code produce an error since the JSON response structure is different than ResponseObj? Or will it be fine but ResponseObj won't have properties of "c", "d", and "e" from the JSON response, and property "b" in ResponseObj will have a value of null?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

0

It depends on some circumstance. The fields you want to map must be the same name (or you can provide alternative name for example with @JsonAlias) and type as in the json. You can create ObjectMapper and set the requiered behaviour. If you set the FAIL_ON_UNKNOWN_PROPERTIES to false, then it will ignore the properties in json, which doesnt mapped into your data class. FAIL_ON_MISSING_CREATOR_PROPERTIEScan control the behavior on missing fields like b. There are lot of properties and annotations which can control the behaviour of serialization/deserialization.

zlaval
  • 1,941
  • 1
  • 10
  • 11