Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - RestTemplate launch exception when http 404 happens

I have a rest service which send an 404 error when the resources is not found. Here the source of my controller and the exception which send Http 404.

@Controller @RequestMapping("/site") public class SiteController {      @Autowired     private IStoreManager storeManager;      @RequestMapping(value = "/stores/{pkStore}", method = RequestMethod.GET, produces = "application/json")     @ResponseBody     public StoreDto getStoreByPk(@PathVariable long pkStore) {                Store s = storeManager.getStore(pkStore);         if (null == s) {             throw new ResourceNotFoundException("no store with pkStore : " + pkStore);         }         return StoreDto.entityToDto(s);             } }   @ResponseStatus(value = HttpStatus.NOT_FOUND) public class ResourceNotFoundException extends RuntimeException {            private static final long serialVersionUID = -6252766749487342137L;         public ResourceNotFoundException(String message) {         super(message);     }     } 

When i try to call it with RestTemplate with this code :

ResponseEntity<StoreDto> r = restTemplate.getForEntity(url, StoreDto.class, m);  System.out.println(r.getStatusCode());  System.out.println(r.getBody()); 

I receive this exception :

org.springframework.web.client.RestTemplate handleResponseError ATTENTION: GET request for "http://........./stores/99" resulted in 404 (Introuvable); invoking error handler org.springframework.web.client.HttpClientErrorException: 404 Introuvable 

I was thinking I can explore my responseEntity Object and do some things with the statusCode. But exception is launch and my app go down.

Is there a specific configuration for restTemplate to not send exception but populate my ResponseEntity.

like image 676
loic Avatar asked Apr 24 '13 14:04

loic


People also ask

How do you handle an exception 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.

Why RestTemplate is deprecated?

RestTemplate provides a synchronous way of consuming Rest services, which means it will block the thread until it receives a response. RestTemplate is deprecated since Spring 5 which means it's not really that future proof.

What is RestTemplate getForObject?

The getForObject method fetches the data for the given response type from the given URI or URL template using HTTP GET method. To fetch data for the given key properties from URL template we can pass Object Varargs and Map to getForObject method. The getForObject returns directly the object of given response type.

What is RestTemplate getForEntity?

The getForEntity method retrieves resources from the given URI or URL templates. It returns response as ResponseEntity using which we can get response status code, response body etc. To fetch data on the basis of some key properties, we can send them as path variables.


1 Answers

As far as I'm aware, you can't get an actual ResponseEntity, but the status code and body (if any) can be obtained from the exception:

try {     ResponseEntity<StoreDto> r = restTemplate.getForEntity(url, StoreDto.class, m); } catch (final HttpClientErrorException e) {     System.out.println(e.getStatusCode());     System.out.println(e.getResponseBodyAsString()); } 
like image 62
Squatting Bear Avatar answered Sep 30 '22 10:09

Squatting Bear