Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring RestTemplate - Overriding ResponseErrorHandler

I am calling a ReST service through RestTemplate and trying to override ResponseErrorHandler in Spring 3.2 to handle custom error codes.

CustomResponseErrroHandler

public class MyResponseErrorHandler implements ResponseErrorHandler {

    @Override
    public boolean hasError(ClientHttpResponse response) throws IOException {
        boolean hasError = false;
        int rawStatusCode = response.getRawStatusCode();
        if (rawStatusCode != 200){
            hasError = true;
        }
        return hasError;
     }

    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
        //String body = IOUtils.toString(response.getBody());
        throw new CustomServiceException(response.getRawStatusCode() , "custom Error");
   }
}

Spring framework invokes hasError method but not handleError, so I couldn't throw my custom exception. After delving into Spring RestTemplate source code, I realized that the code in handleResponseError method is causing the issue - It is looking for response.getStatusCode or response.getStatusText and throwing exception (as statusCode/statusText is null when Rest service throws exception) and it never calls either custom implemented or default handleError method in the next line.

Spring RestTemplate source code for handleResponse method:

private void handleResponseError(HttpMethod method, URI url, ClientHttpResponse response) throws IOException {
    if (logger.isWarnEnabled()) {
        try {
            logger.warn(method.name() + " request for \"" + url + "\" resulted in " +
response.getStatusCode() + " (" + response.getStatusText() + "); invoking error handler");
        }
        catch (IOException e) {
            // ignore
        }
    }
    getErrorHandler().handleError(response);
}

FYI, while service throws exception, I can read rawstatuscode but not statuscode from response

How to bypass this framework code and make call my custom handler? Thanks for your help in advance.

like image 333
user3670450 Avatar asked May 23 '14 21:05

user3670450


People also ask

Is RestTemplate being 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.

What exceptions can RestTemplate throw?

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.

How do I handle HttpStatusCodeException?

You should catch a HttpStatusCodeException exception: try { restTemplate. exchange(...); } catch (HttpStatusCodeException exception) { int statusCode = exception. getStatusCode().

Is RestTemplate Exchange blocking?

RestTemplate uses Java Servlet API and is therefore synchronous and blocking.


1 Answers

I don't see your RestTemplate code, but I assume you to set your ResponseErrorHandler for RestTemplate to use like:

RestTemplate restClient = new RestTemplate();
restClient.setErrorHandler(new MyResponseErrorHandler());

The exception is indeed thrown in handleError method. You can find how to throw CustomException using CustomResponseHandler from one of my previous answers.

like image 50
nilesh Avatar answered Oct 22 '22 07:10

nilesh