Java 如何在 Spring RestTemplate 中记录响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3892018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:26:27  来源:igfitidea点击:

How do I log response in Spring RestTemplate?

javaspringrestlogging

提问by rjsang

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

我正在使用 RestTemplate 调用 Web 服务。

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?

如果这无法返回用户 ID,我只会返回 null,但我不知道为什么。如何将实际的 XML 响应输出到日志?

采纳答案by matt b

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.

根据您使用的建立 HTTP 连接的方法,您可以查看在实际 HTTP 连接类中打开日志记录。

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

例如,如果您使用的是 commons HttpClient,则可以设置

log4j.logger.httpclient.wire=DEBUG

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

commons-httpclient 项目在其日志记录实践的文档中一个完整的页面

回答by surajz

Configure your logging as follows:

按如下方式配置您的日志记录:

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

Then use a curl command to see the output, eg

然后使用 curl 命令查看输出,例如

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

默认情况下,restTemplate 使用 HttpURlConnection(通过 SimpleClientHttpRequest),因此您可能需要切换到 jakarta httpclient 以查看日志语句。否则上面的日志配置会向你显示响应

    <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">
...

回答by Mark Hobson

You can use spring-rest-template-loggerto log RestTemplateHTTP traffic.

您可以使用spring-rest-template-logger来记录RestTemplateHTTP 流量。

Add a dependency to your Maven project:

向您的 Maven 项目添加依赖项:

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

Then customize your RestTemplateas follows:

然后RestTemplate按如下方式自定义您的:

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

Ensure that debug logging is enabled in application.properties:

确保在application.properties以下位置启用调试日志记录:

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

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

现在所有 RestTemplate HTTP 流量都将被记录到org.hobsoft.spring.resttemplatelogger.LoggingCustomizer调试级别。

DISCLAIMER: I wrote this library.

免责声明:我写了这个库。

回答by Solanki Vaibhav

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

你不需要写一行代码,你只需要在 application.properties 文件中添加以下属性

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.

使用它,它也会在调试模式下记录 rest 模板调用的请求正文、请求标头、请求 URL 和响应正文。