gpt4 book ai didi

spring - RestTemplate 记录 POST 数据

转载 作者:行者123 更新时间:2023-12-04 10:53:08 24 4
gpt4 key购买 nike

我的 resttemplate.exchange() 在 POST 请求上失败,服务器返回 500 错误。

我尝试将根日志记录级别设置为 DEBUG,但在返回 500 错误之前没有记录任何内容。为了确保我的日志配置正确,我在 resttemplate 调用之前添加了一行

HttpClient client = new DefaultHttpClient();
client.execute(new HttpGet("http://google.com"));

在这种情况下,确实出现了很多日志消息。

那么如何让 RestTemplate 导出调试数据呢?

谢谢

最佳答案

从您的分析来看,您似乎希望 RestTemplate 使用 Apache HttpClient。

但是,默认情况下,Spring RestTemplate 不使用 Apache HttpClient,而是通过 SimpleClientHttpRequestFactory 使用 JDK 工具(java.net.URL#openConnection() 等)。

org.springframework.http.client.support.HttpAccessor声明:

private ClientHttpRequestFactory requestFactory = new
SimpleClientHttpRequestFactory();

据我所知,这个客户端不支持记录请求/响应。

要将 RestTemplate 更改为使用 HttpClient,请尝试以下操作:
new RestTemplate(new HttpComponentsClientHttpRequestFactory());

日志配置然后应该启用类别 org.apache.http.wire在级别 debug用于记录完整的请求/响应。

关于spring - RestTemplate 记录 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16573501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com