为了账号安全,请及时绑定邮箱和手机立即绑定

Spring RestTemplate - 如何启用请求/响应的完整调试/记录?

Spring RestTemplate - 如何启用请求/响应的完整调试/记录?

呼如林 2019-08-15 14:52:37
Spring RestTemplate - 如何启用请求/响应的完整调试/记录?我一直在使用Spring RestTemplate一段时间,当我试图调试它的请求和响应时,我一直碰壁。我基本上希望看到当我使用curl并打开“详细”选项时看到的相同内容。例如 :curl -v http://twitter.com/statuses/public_timeline.rss将显示发送的数据和接收的数据(包括标题,cookie等)。实现此目的的一种方法是实际更改RestTemplate源代码并在那里添加一些额外的日志记录语句,但我会发现这种方法确实是最后的手段。应该有一些方法告诉Spring Web Client / RestTemplate以更友好的方式记录所有内容。我的目标是能够使用以下代码执行此操作:restTemplate.put("http://someurl", objectToPut, urlPathValues);然后在日志文件或控制台中获取相同类型的调试信息(我使用curl)。我相信这对使用Spring RestTemplate但有问题的人来说非常有用。使用curl调试RestTemplate问题不起作用(在某些情况下)。
查看完整描述

3 回答

?
翻翻过去那场雪

TA贡献2065条经验 获得超14个赞

使用一些代码扩展@hstoerr答案:


创建LoggingRequestInterceptor以记录请求响应

public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor {

    private static final Logger log = LoggerFactory.getLogger(LoggingRequestInterceptor.class);

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {

        ClientHttpResponse response = execution.execute(request, body);

        log(request,body,response);

        return response;
    }

    private void log(HttpRequest request, byte[] body, ClientHttpResponse response) throws IOException {
        //do logging
    }}

设置RestTemplate

RestTemplate rt = new RestTemplate();//set interceptors/requestFactoryClientHttpRequestInterceptor ri = new LoggingRequestInterceptor();List<ClientHttpRequestInterceptor> ris = new ArrayList<ClientHttpRequestInterceptor>();ris.add(ri);rt.setInterceptors(ris);rt.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory());


查看完整回答
反对 回复 2019-08-15
?
RISEBY

TA贡献1856条经验 获得超5个赞

在Spring Boot中,你可以通过在属性中设置它(或其他12因子方法)来获得完整的请求/响应


logging.level.org.apache.http=DEBUG

这个输出


-DEBUG .i.c.DefaultHttpClientConnectionOperator : Connecting to localhost/127.0.0.1:41827

-DEBUG .i.c.DefaultHttpClientConnectionOperator : Connection established 127.0.0.1:39546<->127.0.0.1:41827

-DEBUG o.a.http.impl.execchain.MainClientExec   : Executing request POST /v0/users HTTP/1.1

-DEBUG o.a.http.impl.execchain.MainClientExec   : Target auth state: UNCHALLENGED

-DEBUG o.a.http.impl.execchain.MainClientExec   : Proxy auth state: UNCHALLENGED

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> POST /v0/users HTTP/1.1

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> Content-Type: application/json;charset=UTF-8

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> Content-Length: 56

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> Host: localhost:41827

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> Connection: Keep-Alive

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_102)

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> Accept-Encoding: gzip,deflate

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "POST /v0/users HTTP/1.1[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "Content-Type: application/json;charset=UTF-8[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "Content-Length: 56[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "Host: localhost:41827[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_102)[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "{"id":null,"email":"xenoterracide@gmail.com","new":true}"

和回应


-DEBUG .i.c.DefaultHttpClientConnectionOperator : Connecting to localhost/127.0.0.1:41827

-DEBUG .i.c.DefaultHttpClientConnectionOperator : Connection established 127.0.0.1:39546<->127.0.0.1:41827

-DEBUG o.a.http.impl.execchain.MainClientExec   : Executing request POST /v0/users HTTP/1.1

-DEBUG o.a.http.impl.execchain.MainClientExec   : Target auth state: UNCHALLENGED

-DEBUG o.a.http.impl.execchain.MainClientExec   : Proxy auth state: UNCHALLENGED

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> POST /v0/users HTTP/1.1

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> Content-Type: application/json;charset=UTF-8

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> Content-Length: 56

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> Host: localhost:41827

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> Connection: Keep-Alive

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_102)

-DEBUG org.apache.http.headers                  : http-outgoing-0 >> Accept-Encoding: gzip,deflate

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "POST /v0/users HTTP/1.1[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "Content-Type: application/json;charset=UTF-8[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "Content-Length: 56[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "Host: localhost:41827[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_102)[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "[\r][\n]"

-DEBUG org.apache.http.wire                     : http-outgoing-0 >> "{"id":null,"email":"xenoterracide@gmail.com","new":true}"

或者只是logging.level.org.apache.http.wire=DEBUG似乎包含所有相关信息


查看完整回答
反对 回复 2019-08-15
  • 3 回答
  • 0 关注
  • 6535 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号