我正在使用一个ClientHttpRequestInterceptor来报告RestTemplate的请求和响应。如果模板试图使用错误的类反编组响应,则需要记录异常情况下的响应。
以下是拦截器的启用方式:
@Bean
public RestTemplate restTemplate(ReportingConfiguration reportingConfiguration) {
return new RestTemplateBuilder()
.interceptors(new RestTemplateInterceptor())
.build();
}
这里只接受ClientHttpRequestInterceptor接口。 拦截器看起来像这样:
@Override
public ClientHttpResponse intercept(HttpRequest req, byte[] body, ClientHttpRequestExecution execution) throws IOException {
...
ClientHttpResponse response = null;
try {
response = execution.execute(request, body);
} catch (Exception e) {
System.out.println("I need to get the response here");
}
...
我知道使用这个类是无法获取响应的,能否提供一种替代方案?
以下是异常示例:(请不要解释如何消除此异常,我知道为什么会出现这种情况,但我想在发生这种情况时记录有效负载)
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 未识别字段“items”(类com.exapmle.group.web.beans.Response),没有标记为可忽略的(已知属性有:“text”,“status”,“set-cookies”)
at [Source: (ByteArrayInputStream); line: 1, column: 11] (through reference chain: com.exapmle.group.web.beans.Response["items"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)