【问题标题】:How to debug MVC Controller URL mapping in Spring Boot?如何在 Spring Boot 中调试 MVC 控制器 URL 映射?
【发布时间】:2018-09-24 09:46:54
【问题描述】:

我有一个运行 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: [/*]

但我看不到 Controller 类使用 handlerMapping 进行映射。调用 http://localhost:8080 只会给我一个服务器不可用错误。

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

  1. Controller 类位于主 Application.java 的子文件夹中 文件。
  2. Controller 有@Controller 注解。该包中的其他@Components 正在自动装配,所以我确信该文件夹是 扫描。
  3. 我在主类中扩展了 SpringBootServletInitializer 类。
  4. 主类中有如下注解 -@SpringBootApplication @Import(PersistenceConfig.class)、@EnableKafka、@EnableCaching、@EnableWebMvc、 @EnableAutoConfiguration(排除 = { KafkaAutoConfiguration.class }) 这里 PersistenceConfig 是我自己的类。

有没有办法找出为什么 MVC 没有将 Controller 类添加到 URL 映射?

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

【问题讨论】:

  • 可以通过创建mvce来共享代码
  • @AbhijitSarkar 我可以使用 JSP 和 tomcat 获得简单的 Spring Boot 应用程序。问题在于我现有的项目有很多组件 - Kafka 消费者/生产者、JPA 等。如果有办法跟踪组件扫描或 MVC 初始化过程,我想我会有所了解。
  • 可以得到applicationContext = SpringApplication.run(Application.class, args);然后 String[] allBeanNames = applicationContext.getBeanDefinitionNames();查看是否已加载。或者使用 actuator org.springframework.bootspring-boot-starter-actuator Spring Boot Actuator 功能提供了用于监控我们应用程序的端点统计数据。
  • @VikramPalakurthi 谢谢。你的回答让我更接近这个问题。当我尝试打印 bean 名称时,我看到 run() 方法没有返回 bean 名称,因为上下文一直在加载。问题是有一个 bean 在启动时加载数据,因此上下文尚未准备好为映射 URL 的请求提供服务。

标签: spring-mvc tomcat spring-boot


【解决方案1】:

有没有办法找出为什么 MVC 没有将 Controller 类添加到 URL 映射?

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

设置logEffectiveWebXml="true" on your context 并在您的日志中查看有效的web.xml - 从那里您应该能够确定URL 处理是否 已连接,或者可能过载或以其他方式被抢占。

【讨论】:

  • 在 Spring Boot 的嵌入式 Tomcat 中启用此功能的配置属性是什么?
【解决方案2】:

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

这是因为 ApplicationContext 仍在加载。应用程序上下文正在等待一个 bean 初始化,该初始化需要很长时间才能从数据库中加载一个 hashmap。所以上下文还没有准备好为映射 URL 的请求提供服务。

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

删除慢 bean 后,我可以访问 URL。感谢所有输入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-16
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    相关资源
    最近更新 更多