Writing RestTemplate POST Data

my resttemplate.exchange () failed to complete the POST request, and the server returned an error of 500.

I tried setting the root logging level to DEBUG, but nothing was logged before error 500 was returned. To make sure my logging configuration was correct, I added a line before calling resttemplate

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

in this case, a lot of registration messages actually appeared.

so how can I get RestTemplate to export debugging data?

Thanks yang

+7
source share
1 answer

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 JDK objects (java.net.URL # openConnection (), etc.) using SimpleClientHttpRequestFactory.

org.springframework.http.client.support.HttpAccessor declares:

 private ClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); 

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

To change the RestTemplate to use HttpClient, try the following:

 new RestTemplate(new HttpComponentsClientHttpRequestFactory()); 

The logging configuration should then include the org.apache.http.wire level org.apache.http.wire category to complete all requests / responses.

+10
source

All Articles