全部
常见问题
产品动态
精选推荐

找回启动日志中的请求路径列表

管理 管理 编辑 删除

如果您对Spring Boot有一些经验,或者对Spring MVC很熟悉。那么对于Spring构建的Web应用在启动的时候,都会输出当前应用创建的HTTP接口列表。

比如下面的这段日志:

2020-02-11 15:32:39.293  INFO 48395 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-11 15:32:39.482  INFO 48395 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7ff95560: startup date [Tue Feb 11 15:32:37 CST 2020]; root of context hierarchy
2020-02-11 15:32:39.568  INFO 48395 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/users/{id}],methods=[GET]}" onto public com.didispace.chapter26.User com.didispace.chapter26.UserController.getUser(java.lang.Long)
2020-02-11 15:32:39.569  INFO 48395 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/users/{id}],methods=[PUT]}" onto public java.lang.String com.didispace.chapter26.UserController.putUser(java.lang.Long,com.didispace.chapter26.User)
2020-02-11 15:32:39.570  INFO 48395 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/users/],methods=[GET]}" onto public java.util.List com.didispace.chapter26.UserController.getUserList()
2020-02-11 15:32:39.570  INFO 48395 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/users/],methods=[POST]}" onto public java.lang.String com.didispace.chapter26.UserController.postUser(com.didispace.chapter26.User)
2020-02-11 15:32:39.570  INFO 48395 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/users/{id}],methods=[DELETE]}" onto public java.lang.String com.didispace.chapter26.UserController.deleteUser(java.lang.Long)
2020-02-11 15:32:39.573  INFO 48395 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2020-02-11 15:32:39.573  INFO 48395 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2020-02-11 15:32:39.590  INFO 48395 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-02-11 15:32:39.590  INFO 48395 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

这些日志接口信息是由org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping类在启动的时候,通过扫描Spring MVC的@Controller@RequestMapping等注解去发现应用提供的所有接口信息。然后在日志中打印,以方便开发者排查关于接口相关的启动是否正确。

但是,足够仔细的开发者可能已经发现,从Spring Boot 2.1.0版本开始,就不再打印这些信息了,完整的启动日志变的非常少,以Spring Boot 2.x基础教程:构建RESTful API与单元测试一文的例子为例,可以看到输入内容如下:

2020-02-11 15:34:15.280  INFO 48784 --- [main] c.d.chapter26.Chapter26Application       : Starting Chapter26Application on zhaiyongchaodeMacBook-Pro.local with PID 48784 (/Users/zhaiyongchao/Documents/git/github/SpringBoot-Learning/2.1.x/chapter2-6/target/classes started by zhaiyongchao in /Users/zhaiyongchao/Documents/git/github/SpringBoot-Learning/2.1.x)
2020-02-11 15:34:15.283  INFO 48784 --- [main] c.d.chapter26.Chapter26Application       : No active profile set, falling back to default profiles: default
2020-02-11 15:34:16.556  INFO 48784 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-02-11 15:34:16.587  INFO 48784 --- [main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-02-11 15:34:16.588  INFO 48784 --- [main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2020-02-11 15:34:16.596  INFO 48784 --- [main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/zhaiyongchao/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2020-02-11 15:34:16.702  INFO 48784 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-02-11 15:34:16.702  INFO 48784 --- [main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1377 ms
2020-02-11 15:34:16.954  INFO 48784 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-02-11 15:34:17.187  INFO 48784 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-02-11 15:34:17.192  INFO 48784 --- [main] c.d.chapter26.Chapter26Application       : Started Chapter26Application in 2.238 seconds (JVM running for 2.764)

找回日志中请求路径列表

为什么在Spring Boot 2.1.x版本中不再打印请求路径列表呢?

主要是由于从该版本开始,将这些日志的打印级别做了调整:从原来的INFO调整为TRACE。所以,当我们希望在应用启动的时候打印这些信息的话,只需要在配置文件增增加对RequestMappingHandlerMapping类的打印级别设置即可,比如在application.properties中增加下面这行配置:

logging.level.org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping=trace

在增加了上面的配置之后重启应用,便可以看到如下的日志打印:

2020-02-11 15:36:09.787 TRACE 49215 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : 
	c.d.c.UserController:
	{PUT /users/{id}}: putUser(Long,User)
	{GET /users/{id}}: getUser(Long)
	{POST /users/}: postUser(User)
	{GET /users/}: getUserList()
	{DELETE /users/{id}}: deleteUser(Long)
2020-02-11 15:36:09.791 TRACE 49215 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : 
	o.s.b.a.w.s.e.BasicErrorController:
	{ /error}: error(HttpServletRequest)
	{ /error, produces [text/html]}: errorHtml(HttpServletRequest,HttpServletResponse)
2020-02-11 15:36:09.793 DEBUG 49215 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : 7 mappings in 'requestMappingHandlerMapping'

可以看到在2.1.x版本之后,除了调整了日志级别之外,对于打印内容也做了调整。现在的打印内容根据接口创建的Controller类做了分类打印,这样更有助于开发者根据自己编写的Controller来查找初始化了那些HTTP接口。


注:本文转载自“程序猿DD”,如有侵权,请联系删除!

请登录后查看

吴汐 最后编辑于2024-12-18 14:48:23

快捷回复
回复
回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
排序 默认正序 回复倒序 点赞倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推荐': '推荐'}}
{{item.is_suggest == 1? '取消推荐': '推荐'}}
沙发 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暂无简介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
查看更多
134
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

相关推荐

快速安全登录

使用微信扫码登录
{{item.label}} 加精
{{item.label}} {{item.label}} 板块推荐 常见问题 产品动态 精选推荐 首页头条 首页动态 首页推荐
取 消 确 定
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录/注册
暂不绑定
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 源码下载 CRMEB帮助文档 帮助文档
返回顶部 返回顶部
CRMEB客服