Spring MVC - RestTemplate launch exception when http 404 happens

前端 未结 5 1248
野性不改
野性不改 2020-12-30 19:48

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.

@Contr         


        
相关标签:
5条回答
  • 2020-12-30 20:26

    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());
    }
    
    0 讨论(0)
  • Recently had a usecase for this. My solution:

    public class MyErrorHandler implements ResponseErrorHandler {
    
    @Override
    public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
        return hasError(clientHttpResponse.getStatusCode());
    }
    
    @Override
    public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
        HttpStatus statusCode = clientHttpResponse.getStatusCode();
        MediaType contentType = clientHttpResponse
            .getHeaders()
            .getContentType();
        Charset charset = contentType != null ? contentType.getCharset() : null;
        byte[] body = FileCopyUtils.copyToByteArray(clientHttpResponse.getBody());
    
        switch (statusCode.series()) {
            case CLIENT_ERROR:
                throw new HttpClientErrorException(statusCode, clientHttpResponse.getStatusText(), body, charset);
            case SERVER_ERROR:
                throw new HttpServerErrorException(statusCode, clientHttpResponse.getStatusText(), body, charset);
            default:
                throw new RestClientException("Unknown status code [" + statusCode + "]");
        }
    
    }
    
    private boolean hasError(HttpStatus statusCode) {
        return (statusCode.series() == HttpStatus.Series.CLIENT_ERROR ||
            statusCode.series() == HttpStatus.Series.SERVER_ERROR);
    }
    
    0 讨论(0)
  • 2020-12-30 20:32

    RESTTemplate is quite deficient in this area IMO. There's a good blog post here about how you could possibly extract the response body when you've received an error:

    http://springinpractice.com/2013/10/07/handling-json-error-object-responses-with-springs-resttemplate

    As of today there is an outstanding JIRA request that the template provides the possibility to extract the response body:

    https://jira.spring.io/browse/SPR-10961

    The trouble with Squatting Bear's answer is that you would have to interrogate the status code inside the catch block eg if you're only wanting to deal with 404's

    Here's how I got around this on my last project. There may be better ways, and my solution doesn't extract the ResponseBody at all.

    public class ClientErrorHandler implements ResponseErrorHandler
    {
       @Override
       public void handleError(ClientHttpResponse response) throws IOException 
       {
           if (response.getStatusCode() == HttpStatus.NOT_FOUND)
           {
               throw new ResourceNotFoundException();
           }
    
           // handle other possibilities, then use the catch all... 
    
           throw new UnexpectedHttpException(response.getStatusCode());
       }
    
       @Override
       public boolean hasError(ClientHttpResponse response) throws IOException 
       {
           return response.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR
             || response.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR;
       }
    

    The ResourceNotFoundException and UnexpectedHttpException are my own unchecked exceptions.

    The when creating the rest template:

        RestTemplate template = new RestTemplate();
        template.setErrorHandler(new ClientErrorHandler());
    

    Now we get the slightly neater construct when making a request:

        try
        {
            HttpEntity response = template.exchange("http://localhost:8080/mywebapp/customer/100029",
                                            HttpMethod.GET, requestEntity, String.class);
            System.out.println(response.getBody());
        }
        catch (ResourceNotFoundException e)
        {
            System.out.println("Customer not found");
        }
    
    0 讨论(0)
  • 2020-12-30 20:32

    You can create your own RestTemplate wrapper which does not throw exceptions, but returns a response with the received status code. (You could also return the body, but that would stop being type-safe, so in the code below the body remains simply null.)

    /**
     * A Rest Template that doesn't throw exceptions if a method returns something other than 2xx
     */
    public class GracefulRestTemplate extends RestTemplate {
        private final RestTemplate restTemplate;
    
        public GracefulRestTemplate(RestTemplate restTemplate) {
            super(restTemplate.getMessageConverters());
            this.restTemplate = restTemplate;
        }
    
        @Override
        public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException {
            return withExceptionHandling(() -> restTemplate.getForEntity(url, responseType));
        }
    
        @Override
        public <T> ResponseEntity<T> postForEntity(URI url, Object request, Class<T> responseType) throws RestClientException {
            return withExceptionHandling(() -> restTemplate.postForEntity(url, request, responseType));
        }
    
        private <T> ResponseEntity<T> withExceptionHandling(Supplier<ResponseEntity<T>> action) {
            try {
                return action.get();
            } catch (HttpClientErrorException ex) {
                return new ResponseEntity<>(ex.getStatusCode());
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 20:38

    Since it's 2018 and I hope that when people say "Spring" they actually mean "Spring Boot" at least, I wanted to expand the given answers with a less dust-covered approach.

    Everything mentioned in the previous answers is correct - you need to use a custom ResponseErrorHandler. Now, in Spring Boot world the way to configure it is a bit simpler than before. There is a convenient class called RestTemplateBuilder. If you read the very first line of its java doc it says:

    Builder that can be used to configure and create a RestTemplate. Provides convenience methods to register converters, error handlers and UriTemplateHandlers.

    It actually has a method just for that:

    new RestTemplateBuilder().errorHandler(new DefaultResponseErrorHandler()).build();
    

    On top of that, Spring guys realized the drawbacks of a conventional RestTemplate long time ago, and how it can be especially painful in tests. They created a convenient class, TestRestTemplate, which serves as a wrapper around RestTemplate and set its errorHandler to an empty implementation:

    private static class NoOpResponseErrorHandler extends 
           DefaultResponseErrorHandler {
    
        @Override
        public void handleError(ClientHttpResponse response) throws IOException {
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题