15

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?

rjsang
  • 1,757
  • 3
  • 16
  • 26
  • You can also use LoggingRequestInterceptor cf http://stackoverflow.com/a/22620168/409784 – Francois Jan 28 '15 at 16:05
  • Resolved https://stackoverflow.com/questions/7952154/spring-resttemplate-how-to-enable-full-debugging-logging-of-requests-responses/47467572 – user2746033 Nov 24 '17 at 06:43

5 Answers5

11

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
  • 138,234
  • 66
  • 282
  • 345
  • 1
    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. – rjsang Oct 11 '10 at 15:35
  • 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 Oct 11 '10 at 17:25
  • Sorry, this is old but I forgot to tick this as the correct answer! – rjsang Dec 21 '10 at 09:02
9

Configure your logging as follows:

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

Then 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">
...
Marvo
  • 17,845
  • 8
  • 50
  • 74
surajz
  • 3,471
  • 3
  • 32
  • 38
  • 3
    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 – rjsang Oct 11 '10 at 15:32
  • There are Windows versions of curl, and also cygwin versions: http://curl.haxx.se/download.html#Win32 – matt b Oct 11 '10 at 17:25
3

You can use spring-rest-template-logger to log RestTemplate HTTP traffic.

Add a dependency to your Maven project:

<dependency>
    <groupId>org.hobsoft.spring</groupId>
    <artifactId>spring-rest-template-logger</artifactId>
    <version>2.0.0</version>
</dependency>

Then customize your RestTemplate as follows:

RestTemplate restTemplate = new RestTemplateBuilder()
    .customizers(new LoggingCustomizer())
    .build()

Ensure that debug logging is enabled in application.properties:

logging.level.org.hobsoft.spring.resttemplatelogger.LoggingCustomizer = DEBUG

Now all RestTemplate HTTP traffic will be logged to org.hobsoft.spring.resttemplatelogger.LoggingCustomizer at debug level.

DISCLAIMER: I wrote this library.

Mark Hobson
  • 1,591
  • 1
  • 12
  • 11
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/17802986) – Al-Mothafar Nov 01 '17 at 10:53
  • @Al-Mothafar Thanks, I've included the relevant information here. – Mark Hobson Nov 01 '17 at 11:41
0

If you used swagger to generate the RestTemplate based client then in the ApiClient there is a public method setDebugging and you can set to true or false and then I was able to see whats going on. Hope this helps. I used latest swager generator cli I think its 2.9 or something

kedar
  • 41
  • 2
-3

you don't need to write a single line of code, you just need to add the following property in application.properties file

logging.level.org.springframework.web.client.RestTemplate=DEBUG

using this it will log rest template call's request body, request header, request URL, and response body also in debug mode.

Solanki Vaibhav
  • 449
  • 6
  • 10