Spring Boot RestTemplate ClientHttpRequestInterceptor日志响应正文(在发生异常的情况下)>

问题描述 投票:0回答:1

我正在使用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的请求输出和响应输入。如果发生异常,我需要记录响应,比如说模板是否试图解散...

java spring spring-boot resttemplate interceptor
1个回答
0
投票

这应该起作用:

© www.soinside.com 2019 - 2024. All rights reserved.