RestTemplate记录POST数据

ted*_*ddy 7 spring log4j resttemplate

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

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

HttpClient client = new DefaultHttpClient();
client.execute(new HttpGet("http://google.com"));
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢杨

Gee*_*ert 11

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

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

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

private ClientHttpRequestFactory requestFactory = new
SimpleClientHttpRequestFactory();
Run Code Online (Sandbox Code Playgroud)

据我所知,这个客户端不支持记录请求/响应.

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

new RestTemplate(new HttpComponentsClientHttpRequestFactory());
Run Code Online (Sandbox Code Playgroud)

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