Spring RestTemplate - 如何启用完整的调试/记录请求/响应? [英] Spring RestTemplate - how to enable full debugging/logging of requests/responses?

查看:939
本文介绍了Spring RestTemplate - 如何启用完整的调试/记录请求/响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Spring RestTemplate一段时间,当我尝试调试它的请求和响应时,我一直在墙上。我基本上看到与我看到的相同的事情,当我使用卷曲与详细选项打开。例如:

I have been using the Spring RestTemplate for a while and I consistently hit a wall when I'am trying to debug it's requests and responses. I'm basically looking to see the same things as I see when I use curl with the "verbose" option turned on. For example :

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

将显示发送的数据和收到的数据(包括标题,Cookie等)。

Would display both the sent data and the received data (including the headers, cookies, etc.).

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

I have checked some related posts like : How do I log response in Spring RestTemplate? but I haven't managed to solve this issue.

一种方法这样做是为了实际改变RestTemplate源代码,并在那里添加一些额外的日志记录,但我会发现这个方法真的是一个最后的手段。应该有一些方法来告诉Spring Web Client / RestTemplate以更友好的方式记录所有内容。

One way to do this would be to actually change the RestTemplate source code and add some extra logging statements there, but I would find this approach really a last resort thing. There should be some way to tell Spring Web Client/RestTemplate to log everything in a much friendlier way.

我的目标是通过以下代码来实现:

My goal would be to be able to do this with code like :

restTemplate.put("http://someurl", objectToPut, urlPathValues);

然后在日志文件中获取相同类型的调试信息(和curl一样)或在控制台中。
我相信这对使用Spring RestTemplate并且有问题的任何人来说都是非常有用的。使用curl调试您的RestTemplate问题不起作用(在某些情况下)。

and then to get the same type of debug information (as I get with curl) in the log file or in the console. I believe this would be extremely useful for anyone that uses the Spring RestTemplate and has problems. Using curl to debug your RestTemplate problems just doesn't work (in some cases).

推荐答案

我终于找到了一种以正确的方式做到这一点的方法。
大多数解决方案来自
如何配置Spring和SLF4J以便我可以获取日志记录?

I finally found a way to do this in the right way. Most of the solution comes from How do I configure Spring and SLF4J so that I can get logging?

似乎有两件事情需要要完成:

It seems there are two things that need to be done :


  1. 在log4j.properties中添加以下行: log4j.logger.httpclient.wire = DEBUG

  2. 确保spring不会忽略您的日志记录配置

第二个问题主要发生在使用slf4j的弹簧环境(就像我的情况)。
因此,当使用slf4j时,确保发生以下两件事情:

The second issue happens mostly to spring environments where slf4j is used (as it was my case). As such, when slf4j is used make sure that the following two things happen :


  1. 没有公共 - 您的类路径中的日志库:可以通过在pom中添加排除描述符来完成:

  1. There is no commons-logging library in your classpath : this can be done by adding the exclusion descriptors in your pom :

        <exclusions><exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
    </exclusions>


  • log4j.properties文件存储在spring中找到/看到的类路径中的某个位置。如果您有这个问题,最后的解决方案是将log4j.properties文件放在默认包中(不是一个很好的做法,但只是看到这些工作按照你的期望)

  • The log4j.properties file is stored somewhere in the classpath where spring can find/see it. If you have problems with this, a last resort solution would be to put the log4j.properties file in the default package (not a good practice but just to see that things work as you expect)

    这篇关于Spring RestTemplate - 如何启用完整的调试/记录请求/响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆