Spring Boot RestTemplate 自定义返回码异常处理

HTTP返回场景

返回状态:417 Expectation Failed
返回内容:

{
    "object": "error",
    "code": "charge_id_not_exist",
    "message": "Charge ID does not exist or invalid"
}

默认的DefaultResponseErrorHandler会报HttpClientErrorException错误,而且取不到返回body内容

org.springframework.web.client.HttpClientErrorException: 417 null
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:579)

自定义返回码异常处理

@Configuration
public class CommonConfig {

    @Bean
    //@LoadBalanced   //增加 load balance 特性
    public RestTemplate restTemplate() {
        ClientHttpRequestFactory factory = simpleClientHttpRequestFactory();
        RestTemplate restTemplate = new RestTemplate(factory);
        restTemplate.setErrorHandler(new AppBlogResponseErrorHandler());
        return restTemplate;
    }

    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(15000);  //单位为ms
        factory.setConnectTimeout(15000);  //单位为ms
        return factory;
    }
}
public class AppBlogResponseErrorHandler extends DefaultResponseErrorHandler {

    @Override
    public boolean hasError(ClientHttpResponse response) throws IOException {
        // check if response code is an error in here or just use default implementation
        int rawStatusCode = response.getRawStatusCode();
        HttpStatus statusCode = HttpStatus.resolve(rawStatusCode);
        return statusCode != null ? this.hasError(statusCode) : this.hasError(rawStatusCode);
    }

    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
        // handle different response codes
        // (default spring behaviour is throwing an exception)
        HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode());
        if (statusCode == null) {
            throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), this.getResponseBody(response), this.getCharset(response));
        } else {
            this.handleError(response, statusCode);
        }
    }

    protected boolean hasError(HttpStatus statusCode) {
        return statusCode.is5xxServerError();
    }
}

参考:https://stackoverflow.com/questions/50424852/spring-resttemplate-exchange-post-httpclientexception-with-any-non-200-ok-respon

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/19/spring-boot-resttemplate-custom-return-code-exception-handling/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Spring Boot RestTemplate 自定义返回码异常处理
HTTP返回场景 返回状态:417 Expectation Failed 返回内容: { "object": "error", "code": "charge_id_not_exi……
<<上一篇
下一篇>>
文章目录
关闭
目 录