偵錯 Spring RestTemplate 請求和回應可能是一項令人困惑的任務。本文探討了克服這項挑戰的有效解決方案,提供了一種全面的方法來啟用完整的日誌記錄和偵錯功能。
與使用帶有「詳細」功能的curl相比選項,Spring RestTemplate 通常提供對請求和回應細節的有限洞察。這種可見性的缺乏阻礙了高效的調試和故障排除。
為了解決這個問題,Spring Web Client 和 RestTemplate 提供了 ClientHttpRequestInterceptor 介面。透過實作此接口,開發人員可以自訂 RestTemplate 實例的行為,包括追蹤傳出請求和傳入回應的能力。
要實作ClientHttpRequestInterceptor,請考慮以下內容範例:
import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor { final static Logger log = LoggerFactory.getLogger(LoggingRequestInterceptor.class); @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { traceRequest(request, body); ClientHttpResponse response = execution.execute(request, body); traceResponse(response); return response; } private void traceRequest(HttpRequest request, byte[] body) throws IOException { log.info("===========================request begin================================================"); log.debug("URI : {}", request.getURI()); log.debug("Method : {}", request.getMethod()); log.debug("Headers : {}", request.getHeaders() ); log.debug("Request body: {}", new String(body, "UTF-8")); log.info("==========================request end================================================"); } private void traceResponse(ClientHttpResponse response) throws IOException { StringBuilder inputStringBuilder = new StringBuilder(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), "UTF-8")); String line = bufferedReader.readLine(); while (line != null) { inputStringBuilder.append(line); inputStringBuilder.append('\n'); line = bufferedReader.readLine(); } log.info("============================response begin=========================================="); log.debug("Status code : {}", response.getStatusCode()); log.debug("Status text : {}", response.getStatusText()); log.debug("Headers : {}", response.getHeaders()); log.debug("Response body: {}", inputStringBuilder.toString()); log.info("=======================response end================================================="); } }
利用攔截器,使用FactorBquestingClient實例化RestTemplate 並註冊LoggingRequestInterceptor,如下所示:
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); interceptors.add(new LoggingRequestInterceptor()); restTemplate.setInterceptors(interceptors);
BufferingClientHttpRequestFactory 是啟用回應正文的多次讀取所必需的。
透過實作ClientHttpRequestInterceptor,可以增強Spring的調試能力休息模板。本文提供的範例示範如何追蹤請求和回應,從而提供對通訊過程的全面見解。這種方法簡化了故障排除並提高了開發和維護任務的效率。
以上是如何追蹤 Spring RestTemplate 請求和回應:綜合指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!