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.
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.
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.
You should catch a HttpStatusCodeException exception: try { restTemplate. exchange(...); } catch (HttpStatusCodeException exception) { int statusCode = exception. getStatusCode().
RestTemplate uses Java Servlet API and is therefore synchronous and blocking.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With