如何记录 Spring RestTemplate 请求和响应以进行有效调试?
调试 Spring RestTemplate 请求和响应:综合指南
调试 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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

在使用IntelliJIDEAUltimate版本启动Spring...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

Redis缓存方案如何实现产品排行榜列表的需求?在开发过程中,我们常常需要处理排行榜的需求,例如展示一个�...
