调试 Spring RestTemplate 时,检查请求和响应可以提供宝贵的见解。要使用“-v”选项复制curl命令的详细输出,我们需要为RestTemplate启用详细的日志记录或调试。
一种方法是修改RestTemplate的源代码以包含其他日志记录语句,但是不建议将此作为主要解决方案。相反,我们可以利用 ClientHttpRequestInterceptor 的强大功能。
用于请求/响应日志记录的自定义 ClientHttpRequestInterceptor
要记录请求和响应详细信息,我们可以实现自定义 ClientHttpRequestInterceptor:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; 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================================================="); } }
带有缓冲和拦截器的 RestTemplate 配置
要使用我们的自定义拦截器,请按如下方式配置 RestTemplate:
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); interceptors.add(new LoggingRequestInterceptor()); restTemplate.setInterceptors(interceptors);
请注意BufferingClientHttpRequestFactory 是允许拦截器多次访问响应正文所必需的。
用法和预期输出
使用此配置,调用restTemplate.execute()将以所需的格式记录请求和响应详细信息:
restTemplate.put("http://someurl", objectToPut, urlPathValues);
您将在日志中看到以下内容:
===========================request begin=============================================== URI : http://someurl Method : PUT Headers : {...} Request body: {...} ==========================request end=============================================== ============================response begin========================================== Status code : 200 Status text : OK Headers : {...} Response body: {...} =======================response end=================================================
此详细日志记录提供了 RestTemplate 的全面视图交互,使调试显着更加高效和方便。通过避免代码修改并利用提供的可扩展性机制,这种方法为这种常见的调试挑战提供了干净而灵活的解决方案。
以上是如何记录 Spring RestTemplate 请求和响应以进行有效调试?的详细内容。更多信息请关注PHP中文网其他相关文章!