1

Here's my model class:

public class RequestBody {

@JsonProperty("polygon")
private GeoPolygon geoPolygon;

@JsonProperty("domain_id")
private String domainId;

private String availability;

@JsonProperty("start_time")
private String startTime;

@JsonProperty("end_time")
private String endTime;

@JsonProperty("page_size")
private int pageSize;

private int offset;

//getters, setters, toString()

Below is my controller:

@RequestMapping(value = "/request", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity getResponse(RequestBody request){
    // process request, return response.
}

This is how I'm calling the API:

http://localhost:9876/request?availability=TEMPORARY&start_time=2017-06-06T13:24:23Z&end_time=2017-06-05T13:24:23Z&polygon={"type":"polygon","coordinates":[[[-120,10],[-30,10],[-30,60],[-120,60],[-120,10]]]}&domain_id=XYZ&page_size=10&offset=1

Now the issue:

All the properties are not getting mapped. Specially the ones with @JsonProperty annotation. These fields remain null.

I sent the same model to the POST request at the same endpoint and that worked completely fine. Is @JsonProperty not supported with GET?

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91

2 Answers2

3

The @JsonProperty application will be taken into account during JSON serialization/deserialization. JSON deserialization occurs if the extracted content is from the request body (aka @RequestBody Spring annotation).

Your RequestBody (careful with name clashing) is extracted from request parameters. Spring will not use JSON deserialization in that case, but just call the corresponding Java Bean setters.

If you want startTime to be mapped with start_time, your setter must be:

public setStart_time(String startTime) {
    this.startTime = startTime;
}

Same thing for your other fields.

kagmole
  • 2,005
  • 2
  • 12
  • 27
1

As already said in answer by kagmole about difference in JSON deserialization for @RequestBody and RequestParam so you can get rid of error by doing setters on @JsonProperty name.

I just want to add that you should in fact have two getter / setters for your fields if you are not sure to use your POJO for RequestBody or for RequestParam.

This is highlighted in accepted answer & other answers for this SO Question.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98