【问题标题】:Spring boot 404 and still worksSpring Boot 404 仍然有效
【发布时间】:2017-10-13 14:21:08
【问题描述】:

当我将我的 Spring Boot 应用程序作为 JAR 文件运行时,我的所有控制器都可以正常工作(尽管我仍然在日志中得到“没有找到...的处理程序方法”,但是以某种方式适当的控制器和视图呈现得很好。当我在 WAR 模式下运行它,它在日志中给我 404 和相同的错误“没有找到...的处理程序方法”。

这是我的应用程序类:

@SpringBootApplication
public class RAApplication extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    //set register error pagefilter false
    //setRegisterErrorPageFilter(false);
    return application.sources(RAApplication.class);
  }

  public static void main(String[] args) throws Exception {
    SpringApplication.run(RAApplication.class, args);
  }
}

我的主要应用程序类在 com.xyz.abc 下,所有控制器都在 com.xyz.abc.controllers 下,其他所有组件都在 com.xyz.abc 的子包中。

/mappings 和 /beans 工作得很好,而 /mappings 确实显示了这样的内容:

"{[/],methods=[GET]}":{"bean":"requestMappingHandlerMapping","method":"public java.lang.String com.xyz.abc.controllers.HomeController.renderIndexPage(org.springframework.ui.Model)"}

仅供参考,我检查了只有一个 [/] 映射以防它有用。

任何可能出现问题的指针?所有通常的搜索都指向未找到控制器,但 /mappings 显示所有控制器均已正确映射,但为什么它在战争模式下给出 404 并在 jar 模式下出现错误?

谢谢!

更新:我已经设置

servlet.contextPath=/

在 application.properties 中,其他执行器端点也已正确配置,例如

15:21:09.253 [localhost-startStop-1] INFO  org.springframework.boot.web.servlet.FilterRegistrationBean - Mapping filter: 'metricsFilter' to: [/*]
15:21:09.254 [localhost-startStop-1] INFO  org.springframework.boot.web.servlet.FilterRegistrationBean - Mapping filter: 'characterEncodingFilter' to: [/*]
15:21:09.254 [localhost-startStop-1] INFO  org.springframework.boot.web.servlet.FilterRegistrationBean - Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
15:21:09.254 [localhost-startStop-1] INFO  org.springframework.boot.web.servlet.FilterRegistrationBean - Mapping filter: 'httpPutFormContentFilter' to: [/*]
15:21:09.254 [localhost-startStop-1] INFO  org.springframework.boot.web.servlet.FilterRegistrationBean - Mapping filter: 'requestContextFilter' to: [/*]
15:21:09.255 [localhost-startStop-1] INFO  org.springframework.boot.web.servlet.FilterRegistrationBean - Mapping filter: 'webRequestLoggingFilter' to: [/*]
15:21:09.255 [localhost-startStop-1] INFO  org.springframework.boot.web.servlet.FilterRegistrationBean - Filter errorPageFilter was not registered (disabled)
15:21:09.255 [localhost-startStop-1] INFO  org.springframework.boot.web.servlet.FilterRegistrationBean - Mapping filter: 'applicationContextIdFilter' to: [/*]
15:21:09.255 [localhost-startStop-1] INFO  org.springframework.boot.web.servlet.ServletRegistrationBean - Mapping servlet: 'dispatcherServlet' to [/]
15:21:09.289 [localhost-startStop-1] DEBUG org.springframework.boot.web.filter.OrderedRequestContextFilter - Initializing filter 'requestContextFilter'
15:21:09.291 [localhost-startStop-1] DEBUG org.springframework.boot.web.filter.OrderedRequestContextFilter - Filter 'requestContextFilter' configured successfully
15:21:09.291 [localhost-startStop-1] DEBUG org.springframework.boot.web.filter.ApplicationContextHeaderFilter - Initializing filter 'applicationContextIdFilter'
15:21:09.291 [localhost-startStop-1] DEBUG org.springframework.boot.web.filter.ApplicationContextHeaderFilter - Filter 'applicationContextIdFilter' configured successfully

【问题讨论】:

  • 您的 RabbitAssistApplication 和控制器在同一个包中吗?
  • 控制器位于 Application 类所在的子包中。例如com.xyz.abc - 应用程序和 com.xyz.abc.controllers - 控制器
  • 您是否能够看到类似以下 s.w.s.m.m.a.RequestMappingHandlerMapping 的任何日志:将“{[/]}”映射到所有端点的公共
  • @AmitKBist 不,我没有,这就是这个问题的原因。
  • 从 maven 创建战争时,您是否将 spring-boot-starter-tomcat 依赖标记为提供?

标签: spring-mvc spring-boot spring-boot-actuator


【解决方案1】:

如果您将 WAR 部署在 Tomcat 之类的 Web 容器中,那么您就有了上下文路径。

例如 JAR 模式中的 localhost:8080/ 在 Web 容器中变为 localhost:8080/myapp/。

【讨论】:

  • 我在 application.properties 中设置了 servlet.contextPath=/。忘了提,更新了问题。对不起!
  • 但是如何运行 WAR?
猜你喜欢
  • 1970-01-01
  • 2020-01-29
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 2017-04-30
  • 1970-01-01
相关资源
最近更新 更多