0

I am working to hit API through Rest Template and I have successfully received the response in responseEntity form. Now my requirement is to extract values from response to my object.

String uri = "http://192.168.1.113:10100/scf/reset";
String str = "ship";
RestTemplate restTemplate = new RestTemplate(); 
        
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
map.add("scfType", str);

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);

ResponseEntity<String> response = restTemplate.exchange( uri, HttpMethod.POST, request, String.class);

And I have response in the form of JSON

{
    "statusCode": 200,
    "status": "OK",
    "payload": {
        "channelItemId": 41179181,
        "itemQuantity": 6,
        "date_Created": "2022-08-23T21:40:52.412+0000",
        "date_Modified": "2022-09-20T11:15:40.859+0000",
        "deleted": false,
        "user_Id": 2075
}

I have my object which is

ChannelItem channelItem = new ChannelItem();

My requirement is to get values which are in payload and store them in channelItem.

1 Answers1

0

Use ParameterizedTypeReference instead of String
Learn more: Rest Template with ParameterizedTypeReference

ResponseEntity<ChannelItem> response = restTemplate.exchange(uri, HttpMethod.POST, request, new ParameterizedTypeReference<ChannelItem>() {});
ChannelItem channelItem = response.getBody();
Sakil
  • 666
  • 3
  • 8
  • I have tried it already but getting this syntax error. ```The method exchange(String, HttpMethod, HttpEntity>, Class, Object...) in the type RestTemplate is not applicable for the arguments (String, HttpMethod, HttpEntity>, new ParameterizedTypeReference(){})``` – Alyan Ahmed Sep 21 '22 at 09:18