在下面的RestTemplate的errorHandler的简单代码中,我想在日志中包含请求正文
我想保持代码干净,而不要在restTemplate配置类之外使用错误处理程序。因此,我发现的唯一方法是从其余模板调用者/用户WDYT处理它?</ p>
@Configuration
public class HTTPConfiguration {
...
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
if (response.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR) {
LOGGER.error("Server error: {} {}", response.getStatusCode(), response.getStatusText());
} else if (response.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR) {
LOGGER.error("Client error: {} {}", response.getStatusCode(), response.getStatusText());
}
}
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
return (response.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR
|| response.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR);
}
});
...
}
答案 0 :(得分:1)
直接的回答是“否”,*.**.nic.mac1609_4.useAcks = true
是一个接口,只有三种方法,最好的方法是请求url和请求方法docs
handleError(ClientHttpResponse响应)
ResponseErrorHandler
handleError(java.net.URI url,HttpMethod方法,ClientHttpResponse响应)
Handle the error in the given response.
handleError(java.net.URI url,HttpMethod方法,ClientHttpResponse响应)
Alternative to handleError(ClientHttpResponse) with extra information providing access to the request URL and HTTP method.
并且Indicate whether the given response has any errors.
有两个实现类ResponseErrorHandler
,但是没有一个实现类带有DefaultResponseErrorHandler, ExtractingResponseErrorHandler
作为参数DefaultResponseErrorHandler和ExtractingResponseErrorHandler