亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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":"[email protected]","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":"[email protected]","new":true}"

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


查看完整回答
反對 回復 2019-08-15
  • 3 回答
  • 0 關注
  • 6584 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號