Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring restTemplate issue in getting response

My rest server is generating response when I called it with rest client software. When I call it with resttemplate code mentioned above, then server generates response(print logs) but resttemplate does nothing(no next line executes after call) and prints internal error.

This is the method in my server

@ResponseBody
public ResponseEntity<Map<String, Object>> name(){......
...
return new ResponseEntity<Map<String, Object>>(messagebody, HttpStatus.OK);
}

This is the way I am calling it through restTemplate

ResponseEntity<Map> response1 = restTemplate.getForEntity(finalUrl.toString(), Map.class);
like image 366
Muhammad Imran Tariq Avatar asked Mar 04 '15 12:03

Muhammad Imran Tariq


People also ask

How do I get error from RestTemplate?

Default Error Handling By default, the RestTemplate will throw one of these exceptions in the case of an HTTP error: HttpClientErrorException – in the case of HTTP status 4xx. HttpServerErrorException – in the case of HTTP status 5xx. UnknownHttpStatusCodeException – in the case of an unknown HTTP status.

Is RestTemplate getting deprecated?

WebClient offers support for both synchronous and asynchronous HTTP requests and streaming scenarios. Therefore, RestTemplate will be marked as deprecated in a future version of the Spring Framework and will not contain any new functionalities. RestTemplate is based on a thread-per-request model.

How do you get a response body from RestClientException?

Instead of catching RestClientException , catch the special HttpClientErrorException . HttpClientErrorException provides getStatusCode and getResponseBodyAsByteArray to get the status code and body, respectively.

What is difference between getForObject and getForEntity?

So, basically the difference is that, getForEntity() provides more metadata than getForObject().


1 Answers

Try to use ParameterizedTypeReference instead of wildcarded Map. It should looks like this.

ParameterizedTypeReference<Map<String, Object>> typeRef = new ParameterizedTypeReference<Map<String, Object>>() {};

ResponseEntity<Map<String, Object>> response = restTemplate.exchange(finalUrl.toString(), HttpMethod.GET, null, typeRef);
like image 120
Sergey Yamshchikov Avatar answered Sep 22 '22 04:09

Sergey Yamshchikov