Spring RestTemplate returning empty result for JSON response when using custom POJO class

21 views Asked by At

I am getting below JSON response from a REST API end point

{
  "data": {
    "scores": [
      12,
      23
    ]
  },
  "meta": {
    "method": "POST",
    "url": "http://api.io/test/scores",
    "payload": {
      “lookup_scores: [
        “Entry1,
        “Entry2”
      ]
    },
    "timestamp": "2023-10-06T06:02:42.923126",
    "system_latency_ms”:23,
    "user_agent": “testUa”
  }
}

I am using Spring RestTemplate to get the output from an external Rest end point

Below is the code for Rest Template

ResponseEntity<Response> op = restTemplate.exchange("https://api.io/test/scores", HttpMethod.POST, request, Response.class);

I have created corresponding Response POJO class as below :

public class Response {
    private Data data;
    private Meta meta;
}


public class Data {
    private double[] scores;
}


public class Meta {

    private String method;
    private String url;
    private Payload payload;

    private String timestamp;

    private double system_latency_ms;

    private String user_agent;
}


public class Payload {

    private String[] lookup_scores; 
}

The issue I am facing is , when the above method is changed to return String response , the response is returned

ResponseEntity<String> op = restTemplate.exchange("https://api.io/test/scores", HttpMethod.POST, request, String.class);

But when above code is changed to return response in terms of Response object POJO like below -

ResponseEntity<Response> op = restTemplate.exchange("https://api.io/test/scores", HttpMethod.POST, request, Response.class);

The op object returns both “data” and “meta” as null

Can anybody help what is the issue here ? How to resolve this ? I want to get the result in terms of Response POJO object and not the String

0

There are 0 answers