在我的RESTful webservice中,如果出现错误请求(5xx)或4xx响应代码,我会自定义响应头“x-app-err-id”。
在客户端方面,我使用RestTemplate的exchange方法来进行RESTful Web服务调用。当响应代码为2xx时,一切都很好。
ResponseEntity<Component> response = restTemplate.exchange(webSvcURL,
HttpMethod.POST,
requestEntity,
Component.class);
但是如果由于请求不当(5xx)或4xx而出现异常(HttpStatusCodeException),在HttpStatusCodeException的catch块中,我会将响应(见上文)视为null,因此无法访问我在Web服务中设置的自定义标头。如何在RestTemplate中处理异常时获取响应中的自定义标头。
另外一个问题是,如果出现异常(HttpStatusCodeException),我会在响应正文中设置错误对象(json),我想知道如何在RestTemplate中访问响应正文。