Spring RestTemplate - Overriding ResponseErrorHandler

匿名 (未验证) 提交于 2019-12-03 02:15:02

问题:

I am calling Restservice 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 customException. After delving into Spring RestTemplate source code, I realized 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 Sourcecode for handleResponse:

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 customhandler? Thanks for your help in advance.

回答1:

Following link has useful information about Exception Flow for Spring ResponseErrorHandler .

Adding code here, just in-case the blog is down:

Code for ErrorHandler:

public class MyResponseErrorHandler implements ResponseErrorHandler {  private static final Log logger = LogFactory.getLog(MyResponseErrorHandler.class);  @Override public void handleError(ClientHttpResponse clienthttpresponse) throws IOException {      if (clienthttpresponse.getStatusCode() == HttpStatus.FORBIDDEN) {         logger.debug(HttpStatus.FORBIDDEN + " response. Throwing authentication exception");         throw new AuthenticationException();     } }  @Override public boolean hasError(ClientHttpResponse clienthttpresponse) throws IOException {      if (clienthttpresponse.getStatusCode() != HttpStatus.OK) {         logger.debug("Status code: " + clienthttpresponse.getStatusCode());         logger.debug("Response" + clienthttpresponse.getStatusText());         logger.debug(clienthttpresponse.getBody());          if (clienthttpresponse.getStatusCode() == HttpStatus.FORBIDDEN) {             logger.debug("Call returned a error 403 forbidden resposne ");             return true;         }     }     return false; } 

}

Code for using it in RestTemplate:

RestTemplate restclient = new RestTemplate(); restclient.setErrorHandler(new MyResponseErrorHandler()); ResponseEntity<String> responseEntity = clientRestTemplate.exchange(                     URI,                     HttpMethod.GET,                     requestEntity,                     String.class);                 response = responseEntity.getBody(); 


回答2:

I don't see your RestTemlate 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 Spring RestTemplate invoking webservice with errors and analyze status code



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!