RestTemplate记录POST数据 [英] RestTemplate logging POST data

查看:270
本文介绍了RestTemplate记录POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的resttemplate.exchange()在POST请求中失败,服务器返回500错误.

my resttemplate.exchange() failed on a POST request, with the server returning a 500 error.

我尝试将根日志记录级别设置为DEBUG,但是在返回500错误之前未记录任何内容.为了确保我的日志记录配置正确,我在resttemplate调用之前添加了一行

I tried to set the root logging level to DEBUG, but nothing was logged before the 500 error was returned. to make sure that my logging config is right, I added a line before the resttemplate call

HttpClient client = new DefaultHttpClient();
client.execute(new HttpGet("http://google.com"));

在这种情况下,确实出现了许多日志记录消息.

in this case indeed a lot of logging messages appeared.

那么如何使RestTemplate导出调试数据?

so how can I make RestTemplate export the debugging data?

谢谢 杨

推荐答案

从您的分析看来,您似乎希望RestTemplate使用Apache HttpClient.

From your analysis it seems that you expect RestTemplate to use Apache HttpClient.

但是,默认情况下,Spring RestTemplate不使用Apache HttpClient,而是通过SimpleClientHttpRequestFactory使用JDK工具(java.net.URL#openConnection()等).

However, by default, Spring RestTemplate does not use Apache HttpClient but uses the JDK facilities (java.net.URL#openConnection() etc.) by means of SimpleClientHttpRequestFactory.

org.springframework.http .client.support.HttpAccessor 声明:

private ClientHttpRequestFactory requestFactory = new
SimpleClientHttpRequestFactory();

据我所知,此客户端不支持日志记录请求/响应.

As far as I know, this client does not support logging requests/responses.

要将RestTemplate更改为使用HttpClient,请尝试以下操作:

To change RestTemplate to use HttpClient, try this:

new RestTemplate(new HttpComponentsClientHttpRequestFactory());

然后,日志记录配置应在级别debug启用类别org.apache.http.wire,以记录完整的请求/响应.

Logging configuration should then enable category org.apache.http.wire at level debug for complete requests/responses to be logged.

这篇关于RestTemplate记录POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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