猿问

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

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问题不起作用(在某些情况下)。


呼如林
浏览 6536回答 3
3回答

翻翻过去那场雪

使用一些代码扩展@hstoerr答案:创建LoggingRequestInterceptor以记录请求响应public&nbsp;class&nbsp;LoggingRequestInterceptor&nbsp;implements&nbsp;ClientHttpRequestInterceptor&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;final&nbsp;Logger&nbsp;log&nbsp;=&nbsp;LoggerFactory.getLogger(LoggingRequestInterceptor.class); &nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;ClientHttpResponse&nbsp;intercept(HttpRequest&nbsp;request,&nbsp;byte[]&nbsp;body,&nbsp;ClientHttpRequestExecution&nbsp;execution)&nbsp;throws&nbsp;IOException&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ClientHttpResponse&nbsp;response&nbsp;=&nbsp;execution.execute(request,&nbsp;body); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log(request,body,response); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;response; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;void&nbsp;log(HttpRequest&nbsp;request,&nbsp;byte[]&nbsp;body,&nbsp;ClientHttpResponse&nbsp;response)&nbsp;throws&nbsp;IOException&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//do&nbsp;logging &nbsp;&nbsp;&nbsp;&nbsp;}}设置RestTemplateRestTemplate&nbsp;rt&nbsp;=&nbsp;new&nbsp;RestTemplate();//set&nbsp;interceptors/requestFactoryClientHttpRequestInterceptor&nbsp;ri&nbsp;=&nbsp;new&nbsp;LoggingRequestInterceptor();List<ClientHttpRequestInterceptor>&nbsp;ris&nbsp;=&nbsp;new&nbsp;ArrayList<ClientHttpRequestInterceptor>();ris.add(ri);rt.setInterceptors(ris);rt.setRequestFactory(new&nbsp;BufferingClientHttpRequestFactory(new&nbsp;SimpleClientHttpRequestFactory());

RISEBY

在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&nbsp; &nbsp;: Executing request POST /v0/users HTTP/1.1-DEBUG o.a.http.impl.execchain.MainClientExec&nbsp; &nbsp;: Target auth state: UNCHALLENGED-DEBUG o.a.http.impl.execchain.MainClientExec&nbsp; &nbsp;: Proxy auth state: UNCHALLENGED-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> POST /v0/users HTTP/1.1-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> Content-Type: application/json;charset=UTF-8-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> Content-Length: 56-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> Host: localhost:41827-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> Connection: Keep-Alive-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_102)-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> Accept-Encoding: gzip,deflate-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "POST /v0/users HTTP/1.1[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "Content-Type: application/json;charset=UTF-8[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "Content-Length: 56[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "Host: localhost:41827[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_102)[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: 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&nbsp; &nbsp;: Executing request POST /v0/users HTTP/1.1-DEBUG o.a.http.impl.execchain.MainClientExec&nbsp; &nbsp;: Target auth state: UNCHALLENGED-DEBUG o.a.http.impl.execchain.MainClientExec&nbsp; &nbsp;: Proxy auth state: UNCHALLENGED-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> POST /v0/users HTTP/1.1-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> Content-Type: application/json;charset=UTF-8-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> Content-Length: 56-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> Host: localhost:41827-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> Connection: Keep-Alive-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_102)-DEBUG org.apache.http.headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : http-outgoing-0 >> Accept-Encoding: gzip,deflate-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "POST /v0/users HTTP/1.1[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "Content-Type: application/json;charset=UTF-8[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "Content-Length: 56[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "Host: localhost:41827[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_102)[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "[\r][\n]"-DEBUG org.apache.http.wire&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: http-outgoing-0 >> "{"id":null,"email":"xenoterracide@gmail.com","new":true}"或者只是logging.level.org.apache.http.wire=DEBUG似乎包含所有相关信息
随时随地看视频慕课网APP

相关分类

Java
我要回答