文章目录
概述
在Spring Boot项目中,调试Controller接口的请求和响应信息可以极大地帮助开发人员排查问题并确保应用程序的安全性和性能。
实现详细日志输出
1. 调整日志级别
在application.yml
中,通过配置日志级别,可以控制输出的详细程度。对于HTTP请求和响应,可以设置为TRACE
级别,以捕获详细信息:
yaml
logging:
level:
web: TRACE
此配置确保所有请求和响应细节都会被记录,包括请求参数、头信息、和响应状态等。
2. 示例接口
以下是一个简单的Spring Boot接口示例,它会响应GET请求并返回自定义的响应头信息:
java
/**
* 处理GET请求,返回自定义的响应
* 该方法通过@RequestParam接收id和kw作为查询参数,然后构建一个自定义的响应返回给调用者
* 主要用于演示如何在Spring MVC中返回自定义的响应体和响应头
*
* @param id 需要查询的ID
* @return 自定义的响应对象,包含响应体、响应头和HTTP状态码
*/
@GetMapping("/hi3")
public Object index(Long id) {
// 创建HttpHeaders对象,用于自定义响应头
HttpHeaders headers = new HttpHeaders();
// 向响应头中添加自定义的版本信息
headers.add("x-version", "v1");
// 创建响应对象,设置响应体、响应头和HTTP状态码
ResponseEntity<Object> response = new ResponseEntity<>(
// 构建响应体内容,格式化输出id和kw
String.format("id = %d", id),
headers,
HttpStatus.valueOf(200)
);
// 返回自定义的响应对象
return response;
}
此接口接受两个参数id
和kw
,并返回一个包含这些参数的字符串以及自定义头信息的响应。
java
2024-08-09 17:29:09.606 TRACE 23564 --- [nio-7777-exec-3] o.s.web.servlet.DispatcherServlet : GET "/index?id=1&kw=artisan", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
2024-08-09 17:29:09.606 TRACE 23564 --- [nio-7777-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.artisan.controller.ArtisanController#index(Long, String)
2024-08-09 17:29:09.608 TRACE 23564 --- [nio-7777-exec-3] o.s.web.method.HandlerMethod : Arguments: [1, artisan]
2024-08-09 17:29:09.611 DEBUG 23564 --- [nio-7777-exec-3] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.7, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2024-08-09 17:29:09.611 TRACE 23564 --- [nio-7777-exec-3] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing ["【id = 1, kw = artisan】"]
2024-08-09 17:29:09.611 TRACE 23564 --- [nio-7777-exec-3] s.w.s.m.m.a.RequestMappingHandlerAdapter : Applying default cacheSeconds=-1
2024-08-09 17:29:09.611 TRACE 23564 --- [nio-7777-exec-3] o.s.web.servlet.DispatcherServlet : No view rendering, null ModelAndView returned.
2024-08-09 17:29:09.611 DEBUG 23564 --- [nio-7777-exec-3] o.s.web.servlet.DispatcherServlet : Completed 200 OK, headers={masked}
3. 启用请求详细信息日志
为了确保在开发和测试环境中能看到详细的请求信息,可以在application.yml
中设置以下配置:
yaml
spring:
mvc:
log-request-details: true
此配置将启用详细的请求和响应日志输出,包括请求体、响应体、以及头信息等。
java
2024-08-09 18:10:58.126 TRACE 24176 --- [nio-7777-exec-1] o.s.web.servlet.DispatcherServlet : GET "/index?id=1&kw=artisan", parameters={id:[1], kw:[artisan]}, headers={host:[localhost:7777], connection:[keep-alive], cache-control:[max-age=0], sec-ch-ua:["Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"], sec-ch-ua-mobile:[?0], sec-ch-ua-platform:["Windows"], dnt:[1], upgrade-insecure-requests:[1], user-agent:[Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36], accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7], sec-fetch-site:[none], sec-fetch-mode:[navigate], sec-fetch-user:[?1], sec-fetch-dest:[document], accept-encoding:[gzip, deflate, br, zstd], accept-language:[zh-CN,zh;q=0.9], cookie:[SL_G_WPT_TO=en; SL_GWPT_Show_Hide_tmp=undefined; SL_wptGlobTipTmp=undefined]} in DispatcherServlet 'dispatcherServlet'
2024-08-09 18:10:58.130 TRACE 24176 --- [nio-7777-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.artisan.controller.ArtisanController#index(Long, String)
2024-08-09 18:10:58.155 TRACE 24176 --- [nio-7777-exec-1] o.s.web.method.HandlerMethod : Arguments: [1, artisan]
2024-08-09 18:10:58.177 DEBUG 24176 --- [nio-7777-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.7, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2024-08-09 18:10:58.178 TRACE 24176 --- [nio-7777-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing ["【id = 1, kw = artisan】"]
2024-08-09 18:10:58.190 TRACE 24176 --- [nio-7777-exec-1] s.w.s.m.m.a.RequestMappingHandlerAdapter : Applying default cacheSeconds=-1
2024-08-09 18:10:58.190 TRACE 24176 --- [nio-7777-exec-1] o.s.web.servlet.DispatcherServlet : No view rendering, null ModelAndView returned.
2024-08-09 18:10:58.191 DEBUG 24176 --- [nio-7777-exec-1] o.s.web.servlet.DispatcherServlet : Completed 200 OK, headers={x-version:[1.0.0], Content-Type:[text/html;charset=UTF-8], Content-Length:[26], Date:[Fri, 09 Aug 2024 10:10:58 GMT], Keep-Alive:[timeout=60], Connection:[keep-alive]}
注意事项
- 安全性:在生产环境中,应避免输出过于详细的日志,尤其是可能包含敏感信息的日志,以防止潜在的安全风险。
- 性能影响:详细的日志输出可能会对性能产生影响,特别是在高并发的环境下。因此,在生产环境中应慎重使用。
- 日志管理:对于详细日志,应确保有合适的日志管理策略,以便于归档和分析,同时防止磁盘空间被占满。