问题描述
我的RESTTEMPLATE.EXCHANGE()在POST请求上失败,服务器返回500错误.
我试图将根记录级别设置为调试,但是在返回500错误之前,没有记录.为了确保我的记录配置正确,我在RESTTEMPLATE调用之前添加了一行
HttpClient client = new DefaultHttpClient(); client.execute(new HttpGet("http://google.com"));
在这种情况下,确实出现了很多记录消息.
那么如何使RESTTEMPLATE导出调试数据?
谢谢 杨
推荐答案
从您的分析中,您似乎希望RESTTEMPLATE使用Apache httpclient.
但是,默认情况下,spring rettemplate不使用apache httpclient,而是使用JDK设施(Java.net.url#OpenConnection()等)通过SimpleClientHttpRequestFactory.
.client.support.httpaccessor 声明:
private ClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
据我所知,该客户不支持记录请求/响应.
要更改使用httpclient的rettemplate,请尝试以下操作:
new RestTemplate(new HttpComponentsClientHttpRequestFactory());然后,记录配置应启用类别org.apache.http.wire在level debug中以获取完整的请求/响应以记录.
问题描述
my resttemplate.exchange() failed on a POST request, with the server returning a 500 error.
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.
so how can I make RestTemplate export the debugging data?
Thanks Yang
推荐答案
From your analysis it seems that you expect RestTemplate to use Apache HttpClient.
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 declares:
private ClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
As far as I know, this client does not support logging requests/responses.
To change RestTemplate to use HttpClient, try this:
new RestTemplate(new HttpComponentsClientHttpRequestFactory());
Logging configuration should then enable category org.apache.http.wire at level debug for complete requests/responses to be logged.