Search code examples
javaspringspring-bootresttemplateobjectmapper

RestTemplate exchange, values are not mapped for a field with underscore


To consume a rest service, this is the service response

{
   "message": "200",
   "result": "SUCCESS",
   "Test_Id": "23324"
}

Code to consume the service.

ResponseEntity<InfoDto> result = null;
final String uri ="https://app.ed.im/api";

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
result = restTemplate.exchange(uri, HttpMethod.GET, entity, InfoDto.class);

This is the dto

public class InfoDto implements Serializable {
    private String message;
    private String result;
    private String Test_Id;
}

Once this is executed

I had received the values of message and result, but Test_Id value is not mapped.


Solution

  • If there is an issue with parameter name's code convention, add JsonProperty with exact match

    @JsonProperty("Test_Id")
    private String Test_Id; // prefer rename to testId
    

    There's probably an underscore to camel-case conversion