How do I log response in Spring RestTemplate?

后端 未结 5 1361
滥情空心
滥情空心 2020-12-09 18:12

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

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

If this fai

相关标签:
5条回答
  • 2020-12-09 18:44

    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

    0 讨论(0)
  • 2020-12-09 18:44

    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.

    0 讨论(0)
  • 2020-12-09 18:55

    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.

    0 讨论(0)
  • 2020-12-09 19:00

    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.

    0 讨论(0)
  • 2020-12-09 19:08

    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">
    ...
    
    0 讨论(0)
提交回复
热议问题