【问题标题】:How do I log response in Spring RestTemplate?如何在 Spring RestTemplate 中记录响应?
【发布时间】:2011-04-22 23:08:20
【问题描述】:

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

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

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

【问题讨论】:

标签: java spring rest logging


【解决方案1】:

如果您使用 swagger 生成基于 RestTemplate 的客户端,那么在 ApiClient 中有一个公共方法 setDebugging,您可以设置为 true 或 false,然后我就能看到发生了什么。 希望这可以帮助。 我使用了最新的 swager generator cli,我认为它是 2.9 或什么的

【讨论】:

    【解决方案2】:

    您可以使用spring-rest-template-logger 记录RestTemplate HTTP 流量。

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

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

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

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

    确保在application.properties 中启用调试日志记录:

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

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

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

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • @Al-Mothafar 谢谢,我已在此处包含相关信息。
    【解决方案3】:

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

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

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

    【讨论】:

    • 是的,这样就可以了。看看吧。
    • 我在我的机器上询问过它只记录 URL、状态码和 JSON 映射模型类名
    【解决方案4】:

    如下配置您的日志记录:

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

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

    curl  -H 'Accept: application/xml' -H 'Content-Type: application/xml' http://localhost:8080/ser/data
    

    默认情况下,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">
    ...
    

    【讨论】:

    • 该 log4j 设置记录请求但不记录响应。我不能使用 curl,因为它是一个 Windows 环境,而且我正在寻找可以作为代码库的一部分提交的东西,以便为我们的集成测试启用日志记录
    • curl有windows版本,也有cygwin版本:curl.haxx.se/download.html#Win32
    【解决方案5】:

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

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

    log4j.logger.httpclient.wire=DEBUG
    

    commons-httpclient 项目有an entire page in the documentation on their logging practices

    【讨论】:

    • 我不知道我在使用什么方法,这都是在 RestTemplate 的引擎盖下。使用 log4j.logger.httpclient.wire 似乎没有任何作用。
    • 我建议首先打开 ALL of Spring 的日志记录,以了解 RestTemplate 背后发生了什么,这可能会让您知道下一步该去哪里。
    • 对不起,这是旧的,但我忘了勾选这个作为正确答案!
    猜你喜欢
    • 2015-10-07
    • 2023-02-02
    • 2018-10-17
    • 2011-12-18
    • 2015-08-07
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多