How do I log response in Spring RestTemplate?

后端 未结 5 1377
滥情空心
滥情空心 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:55

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

    Add a dependency to your Maven project:

    
        org.hobsoft.spring
        spring-rest-template-logger
        2.0.0
    
    

    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.

提交回复
热议问题