程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring RestTemplate-如何启用请求/响应的完整调试/日志记录?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring RestTemplate-如何启用请求/响应的完整调试/日志记录??

开发过程中遇到Spring RestTemplate-如何启用请求/响应的完整调试/日志记录?的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring RestTemplate-如何启用请求/响应的完整调试/日志记录?的解决方法建议,希望对你解决Spring RestTemplate-如何启用请求/响应的完整调试/日志记录?有所启发或帮助;

仅为了完成示例ClIEnthttprequesTinterceptor以跟踪请求和响应的完整实现,请执行以下操作:

import java.io.bufferedReader;
import java.io.IOException;
import java.io.inputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.httprequest;
import org.springframework.http.clIEnt.ClIEnthttprequestExecution;
import org.springframework.http.clIEnt.ClIEnthttprequesTinterceptor;
import org.springframework.http.clIEnt.ClIEnthttpResponse;

public class LoggingrequesTinterceptor implements ClIEnthttprequesTinterceptor {

    final static Logger log = LoggerFactory.getLogger(LoggingrequesTinterceptor.class);

    @OverrIDe
    public ClIEnthttpResponsE intercept(httprequest request, byte[] body, ClIEnthttprequestExecution execution) throws IOException {
        tracerequest(request, body);
        ClIEnthttpResponse response = execution.execute(request, body);
        traceResponse(responsE);
        return response;
    }

    private voID tracerequest(httprequest request, byte[] body) throws IOException {
        log.info("===========================request begin================================================");
        log.deBUG("URI         : {}", request.getURI());
        log.deBUG("Method      : {}", request.getmethod());
        log.deBUG("headers     : {}", request.getheaders() );
        log.deBUG("request body: {}", new String(body, "UTF-8"));
        log.info("==========================request end================================================");
    }

    private voID traceResponse(ClIEnthttpResponse responsE) throws IOException {
        @R_675_10495@ngBuilder input@R_675_10495@ngBuilder = new StringBuilder();
        BufferedReader bufferedReader = new BufferedReader(new inputStreamReader(response.getbody(), "UTF-8"));
        @R_675_10495@ng line = bufferedReader.readline();
        while (line != null) {
            input@R_675_10495@ngBuilder.append(linE);
            input@R_675_10495@ngBuilder.append('\n');
            line = bufferedReader.readline();
        }
        log.info("============================response begin==========================================");
        log.deBUG("Status code  : {}", response.getStatusCode());
        log.deBUG("Status text  : {}", response.getStatusText());
        log.deBUG("headers      : {}", response.getheaders());
        log.deBUG("Response body: {}", input@R_675_10495@ngBuilder.to@R_675_10495@ng());
        log.info("=======================response end=================================================");
    }

}

然后RestTemplate使用BufferingClIEnthttprequestFactory和实例化LoggingrequesTinterceptor

RestTemplate restTemplate = new RestTemplate(new BufferingClIEnthttprequestFactory(new SimpleClIEnthttprequestFactory()));
List<ClIEnthttprequesTinterceptor> interceptors = new ArrayList<>();
interceptors.add(new LoggingrequesTinterceptor());
restTemplate.seTinterceptors(interceptors);

BufferingClIEnthttprequestFactory因为我们要在拦截器和初始调用代码中都使用响应主体,所以这是必需的。默认实现只允许读取一次响应主体。

解决方法

spring

我使用Spring RestTemplate已经有一段时间了,当我尝试调试它的请求和响应时,我总是碰壁。我基本上希望看到的是与打开“ verbose”选项时使用curl时看到的相同的东西。例如 :

curl -v http://twitter.com/statuses/public_timeline.rss

将同时显示已发送的数据和已接收的数据(包括标题,cookie等)。

我已经检查了一些相关的帖子,例如: 如何在Spring RestTemplate中记录响应? 但是我还没有解决这个问题。

一种方法是实际更改RestTemplate源代码并在其中添加一些额外的日志记录语句,但是我会发现这种方法确实是不得已的事情。应该有某种方式告诉Spring Web Client / RestTemplate以更友好的方式记录所有内容。

我的目标是能够使用以下代码来做到这一点:

restTemplate.put(“http://someurl",objectToPut,urlPathValues);
然后在日志文件或控制台中获取相同类型的调试信息(与curl相同)。我相信这对于使用Spring RestTemplate并有问题的任何人都是非常有用的。使用curl调试RestTemplate问题根本无法正常工作(在某些情况下)。

大佬总结

以上是大佬教程为你收集整理的Spring RestTemplate-如何启用请求/响应的完整调试/日志记录?全部内容,希望文章能够帮你解决Spring RestTemplate-如何启用请求/响应的完整调试/日志记录?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。