【问题标题】:RestTemplate logging POST dataRestTemplate 记录 POST 数据
【发布时间】:2013-05-10 12:34:23
【问题描述】:

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

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

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

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

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

谢谢 杨

【问题讨论】:

    标签: spring log4j resttemplate


    【解决方案1】:

    根据您的分析,您似乎希望 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());
    

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

    【讨论】:

    • 不确定这是否与版本相关,但为 org.apache.http.wire 设置调试对我来说在 Spring RestTemplate 上不起作用。
    猜你喜欢
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2010-11-02
    相关资源
    最近更新 更多