简体   繁体   English

如何在 Spring RestTemplate 中记录响应?

[英]How do I log response in Spring RestTemplate?

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.如果这无法返回用户 ID,我只会返回 null,但我不知道为什么。 How do I output the actual XML response to a log?如何将实际的 XML 响应输出到日志?

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 项目在其日志记录实践的文档中一个完整的页面

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.默认情况下,restTemplate 使用 HttpURlConnection(通过 SimpleClientHttpRequest),因此您可能需要切换到 jakarta httpclient 以查看日志语句。 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">
...

You can use spring-rest-template-logger to log RestTemplate HTTP traffic.您可以使用spring-rest-template-logger来记录RestTemplate HTTP 流量。

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 RestTemplate as 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.LoggingCustomizer at debug level.现在,所有 RestTemplate HTTP 流量都将在调试级别记录到org.hobsoft.spring.resttemplatelogger.LoggingCustomizer

DISCLAIMER: I wrote this library.免责声明:我写了这个库。

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.如果您使用 swagger 生成基于 RestTemplate 的客户端,那么在 ApiClient 中有一个公共方法 setDebugging ,您可以设置为 true 或 false 然后我就可以看到发生了什么。 Hope this helps.希望这会有所帮助。 I used latest swager generator cli I think its 2.9 or something我使用了最新的 swager generator cli 我认为它是 2.9 或什么的

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 和响应正文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM