我正在使用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"); } ...
我知道在这堂课上不可能得到回应,您能给个替代解决方案吗?
我正在使用ClientHttpRequestInterceptor报告RestTemplate的请求输出和响应输入。如果发生异常,我需要记录响应,比如说模板是否试图解散...
这应该起作用: