RestTemplate - 处理异常中的响应头/正文(RestClientException、HttpStatusCodeException) [英] RestTemplate - Handling response headers/body in Exceptions (RestClientException, HttpStatusCodeException)
问题描述
在我的宁静网络服务中,如果出现错误的请求 (5xx) 或 4xx 响应代码,我会向响应写入一个自定义标头x-app-err-id".
In my restful webservice, in case of bad request (5xx) or 4xx respose codes, I write a custom header "x-app-err-id" to the response.
在客户端,我使用 RestTemplate 的 exchange 方法进行 RestFul 网络服务调用.当响应代码为 2xx 时,一切正常.
On the client side, I use exchange method of RestTemplate to make a RestFul web service call. Everything is fine when the response code is 2xx.
ResponseEntity<Component> response = restTemplate.exchange(webSvcURL,
HttpMethod.POST,
requestEntity,
Component.class);
但是如果由于它是一个错误的请求(5xx)或 4xx 而出现异常(HttpStatusCodeException),则在 HttpStatusCodeException 的 catch 块中,我得到的响应(见上文)为空,因此我无法访问我的我在 Web 服务中设置的自定义标头.如果 RestTemplate 中出现异常,如何从响应中获取自定义标头.
But if there is an exception(HttpStatusCodeException) because of it being a bad request(5xx) or 4xx, in the catch block of HttpStatusCodeException, I get response(see above) as null and so I do not have access to my custom header I set in my web service. How do I get custom headers from the response in case of exceptions in RestTemplate.
还有一个问题是,我在响应正文中设置了一个错误对象(json)以防出错,我想知道在 RestTemplate 中出现异常时如何访问响应正文
One more question is, I set an error object(json) in the reponse body in case of error and I would like to know how to access response body as well in case of exceptions in RestTemplate
推荐答案
我终于使用 ResponseErrorHandler 做到了.
I finally did it using ResponseErrorHandler.
public class CustomResponseErrorHandler implements ResponseErrorHandler {
private static ILogger logger = Logger.getLogger(CustomResponseErrorHandler.class);
private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();
public void handleError(ClientHttpResponse response) throws IOException {
List<String> customHeader = response.getHeaders().get("x-app-err-id");
String svcErrorMessageID = "";
if (customHeader != null) {
svcErrorMessageID = customHeader.get(0);
}
try {
errorHandler.handleError(response);
} catch (RestClientException scx) {
throw new CustomException(scx.getMessage(), scx, svcErrorMessageID);
}
}
public boolean hasError(ClientHttpResponse response) throws IOException {
return errorHandler.hasError(response);
}
}
然后通过如下配置为 RestTemplate 使用这个自定义响应处理程序
And then use this custom response handler for RestTemplate by configuring as shown below
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
<property name="errorHandler" ref="customErrorHandler" />
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
<bean id="customErrorHandler " class="my.package.CustomResponseErrorHandler">
</bean>
这篇关于RestTemplate - 处理异常中的响应头/正文(RestClientException、HttpStatusCodeException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!