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

HTTP返回场景

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

1
2
3
4
5
{
"object": "error",
"code": "charge_id_not_exist",
"message": "Charge ID does not exist or invalid"
}

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

1
2
3
4
5
6
7
8
9
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)

自定义返回码异常处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :