Spring integration: handle http error with oubound gateway

前端 未结 3 1066
星月不相逢
星月不相逢 2021-01-07 12:35

How can I handle exceptions in an http outbound gateway? When i receive status code 500 or 400..., an exception is shown. So What should I do to handle http error using spr

3条回答
  •  攒了一身酷
    2021-01-07 13:25

    If the response status code is in the HTTP series CLIENT_ERROR or SERVER_ERROR, HttpClientErrorException or HttpServerErrorException are thrown respectively. Hence the response doesn't go to reply channel

    Refer DefaultResponseErrorHandler
    Methods: hasError and handleError

    To Handle these exceptions, create your own CustomResponseErrorHandler and override contents of hasError and handlerError methods.

    public class CustomResponseErrorHandler extends DefaultResponseErrorHandler {
    
        @Override
        public boolean hasError(ClientHttpResponse response) throws IOException {
            return hasError(getHttpStatusCode(response));
        }
    
        protected boolean hasError(HttpStatus statusCode) {
            return /*Status Code to be considered as Error*/ ;
        }
    
        @Override
        public void handleError(ClientHttpResponse response) throws IOException { /* Handle Exceptions */
        }
    }  
    

    And add error-handler to your http-outbound-gateway

    
    
    

提交回复
热议问题