views:

69

answers:

2

I am using RestTemplate to make calls to a web service.

String userId = restTemplate.getForObject(createUserUrl, String.class);

If this fails to return a user ID I just get returned null but I don't know why. How do I output the actual XML response to a log?

+1  A: 

don't setting log in debug mode produce any output

log4j.logger.org.springframework.web.client=DEBUG

Or use a curl command to see the output, eg

curl  -H 'Accept: application/xml' -H 'Content-Type: application/xml' http://localhost:8080/ser/data

by default, restTemplate uses HttpURlConnection( via SimpleClientHttpRequest), so you might need to switch to jakarta httpclient to see the log statement. Otherwise the above log configuration will out show you the response

    <bean id="httpClientFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory">
        <constructor-arg><bean  class="org.apache.commons.httpclient.HttpClient"/></constructor-arg>
    </bean>
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <constructor-arg ref="httpClientFactory"/>
        <property name="messageConverters">
...
surajz
That log4j setting logs the requests but not the responses. I can't use curl because it is a Windows environment, plus I'm looking for something I could commit as part of the codebase so that the logging is enabled for our integration tests
Rob
There are Windows versions of curl, and also cygwin versions: http://curl.haxx.se/download.html#Win32
matt b
+1  A: 

Depending on which method of making the HTTP connection you are using, you could look at turning up the logging within the actual HTTP connection classes.

For example, if you are using commons HttpClient, you can set

log4j.logger.httpclient.wire=DEBUG

The commons-httpclient project has an entire page in the documentation on their logging practices.

matt b
I don't know what method I'm using, it's all under the hood of RestTemplate. Using log4j.logger.httpclient.wire doesn't seem to do anything.
Rob
I suggest first turning up logging for ALL of Spring to understand what is going on under the hood of RestTemplate, this might give you an idea of where to go next.
matt b