如何在 Spring 启动中调试 MVC 控制器 URL 映射?



我有一个运行Kafka和JPA的Spring Boot应用程序。我想添加一个管理页面,所以首先添加"spring-boot-starter-web"并添加控制器类。但是,当我启动应用程序时,我可以看到Tomcat服务器已启动并且DispatcherServlet已初始化。

2018-04-13 18:25:29.495  INFO 5512 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$f5f4a697] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 
2018-04-13 18:25:30.584  INFO 5512 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-04-13 18:25:30.604  INFO 5512 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-04-13 18:25:30.607  INFO 5512 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.27
2018-04-13 18:25:33.052  INFO 5512 --- [ost-startStop-1] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded
JARs during scanning can improve startup time and JSP compilation time.
2018-04-13 18:25:33.384  INFO 5512 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-04-13 18:25:33.384 DEBUG 5512 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT] 
2018-04-13 18:25:33.385  INFO 5512 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization comp
leted in 6776 ms
2018-04-13 18:25:33.761  INFO 5512 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-04-13 18:25:33.768  INFO 5512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'metricsFilter' to: [/*]

但是我看不到控制器类与处理程序映射映射。调用 http://localhost:8080 只会给我一个服务器不可用的错误。

我该如何调试?我已经检查了以下内容:

  1. 控制器类位于主应用程序的子文件夹中.java 文件。
  2. 控制器具有@Controller注释。该包中的其他@Components正在自动连接,因此我确定该文件夹是 扫描。
  3. 我在主类中扩展了 SpringBootServletInitializer 类。
  4. 主类中有以下注释 -@SpringBootApplication @Import(PersistenceConfig.class), @EnableKafka, @EnableCaching, @EnableWebMvc, @EnableAutoConfiguration(exclude = { KafkaAutoConfiguration.class }) 这里 PersistenceConfig 是我自己的类。

有没有办法找出为什么 MVC 没有将控制器类添加到 URL 映射中?

我无法共享代码,因为它不是公开的。感谢您对此进行调查。

有没有办法找出为什么 MVC 没有将控制器类添加到 URL 映射中?

总是很难找出为什么没有发生某些事情(除非它阻止启动),但是Tomcat中有一个配置项可能会派上用场来调试此问题:

在您的上下文中设置logEffectiveWebXml="true"并在日志中查看有效的 Web.xml - 从那里您应该能够识别 URL 处理是否有线,或者可能过载或以某种方式被抢占。

此问题已解决。我发现我无法访问执行器或任何映射的 URL。我一直从浏览器看到"页面不可用"错误(不是 404)。

这是因为 ApplicationContext 仍在加载。应用程序上下文正在等待 Bean 初始化,这需要很长时间才能从数据库加载哈希映射。因此,上下文尚未准备好为映射 URL 的请求提供服务。

我在日志中看到Tomcat服务器启动消息时感到困惑。那只是服务器。当我访问 URL 时,Web 应用程序上下文尚未准备就绪。

删除慢豆后,我能够访问 URL。感谢您的所有输入。

最新更新