在Spring RestTemplate中如何记录响应?

15

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

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

如果这个函数无法返回用户ID,我只会得到 null 的返回值,但我不知道原因。如何将实际的 XML 响应输出到日志中?


你还可以使用LoggingRequestInterceptor。请参考https://dev59.com/z2sz5IYBdhLWcg3wWmfL#22620168。 - Francois
已解决 https://dev59.com/z2sz5IYBdhLWcg3wWmfL#47467572 - user2746033
6个回答

12

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

例如,如果您正在使用commons HttpClient,则可以设置

log4j.logger.httpclient.wire=DEBUG

commons-httpclient项目在其文档中有一整页关于其日志记录实践的介绍


1
我不知道我正在使用什么方法,这都是在RestTemplate的引擎下进行的。使用log4j.logger.httpclient.wire似乎没有任何作用。 - rjsang
我建议先将Spring的所有日志记录打开,以了解RestTemplate引擎底层正在发生什么,这可能会给你下一步该去哪里的想法。 - matt b
抱歉,这是旧的,但我忘记将其标记为正确答案! - rjsang

9

按照以下方式配置日志记录:

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

3
那个log4j配置记录了请求但没有记录响应。我不能使用curl因为这是Windows环境,另外我正在寻找作为代码库一部分可以提交的东西,以便日志记录对我们的集成测试启用。 - rjsang
有Windows版本的curl,还有cygwin版本:http://curl.haxx.se/download.html#Win32 - matt b

3
你可以使用 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中。

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


虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面更改,仅链接的答案可能会失效。- 来自审查 - Al-Mothafar
@Al-Mothafar 谢谢,我已经在这里包含了相关信息。 - Mark Hobson

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

0
对于Apache HttpClient v4工厂:
final RestTemplate restTemplate = restTemplateBuilder
    .requestFactory(() -> new HttpComponentsClientHttpRequestFactory())
    .build();

Apache库包含以下内容:
public class ManagedHttpClientConnectionFactory
...
    private final Log log = LogFactory.getLog(DefaultManagedHttpClientConnection.class);
    private final Log headerLog = LogFactory.getLog("org.apache.http.headers");
    private final Log wireLog = LogFactory.getLog("org.apache.http.wire");

所以将日志记录器org.apache.http.headersorg.apache.http.wire定义为DEBUG级别!

-3

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

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

使用此功能,它将在调试模式下记录RestTemplate调用的请求正文、请求头、请求URL和响应正文。

是的,这样做就可以了。检查一下吧。 - Solanki Vaibhav
我在我的机器上进行了测试,它只记录URL、状态码和JSON映射模型类名。 - Chris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接