【问题标题】:404 error using Spring MVC Java Config on JBoss在 JBoss 上使用 Spring MVC Java Config 时出现 404 错误
【发布时间】:2018-10-30 06:10:49
【问题描述】:

我使用 Java Config 编写了一个小型 Spring MVC 应用程序。它在 Tomcat 上运行良好,但在 JBoss EAP 6.2 上却不行。它成功部署在 JBoss 上,但是当我请求 Spring MVC 定义的任何页面并且在浏览器中出现 404 错误时,我会收到此警告。

WARN [org.springframework.web.servlet.PageNotFound] (http-/127.0.0.1:8080-1) No mapping found for HTTP request with URI [/example-web/pages/login.jsp] in DispatcherServlet with name 'dispatcher'

在这里你可以看到我的代码:

public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[] { RootConfiguration.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class[] { WebMvcConfig.class };
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/*" };
}

@Override
protected Filter[] getServletFilters() {
    return new Filter[] { new HiddenHttpMethodFilter() };
}
}

这是我的 Spring MVC 配置:

@EnableWebMvc
@ComponentScan("com.spring.example.w.controller")
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("login").setViewName("login");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/pages/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

和 RootConfig:

@Configuration
@ComponentScan
public class RootConfiguration {
}

在部署期间,我可以在日志中看到请求确​​实被映射:

INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 71) Mapped "{[/start],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.spring.example.w.controller.StartController.handleStart() throws javax.servlet.ServletException,java.io.IOException
INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (ServerService Thread Pool -- 71) Mapped URL path [/login] onto handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 71) Root WebApplicationContext: initialization completed in 2530 ms
INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/example-web]] (ServerService Thread Pool -- 71) Initializing Spring FrameworkServlet 'dispatcher'
INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 71) FrameworkServlet 'dispatcher': initialization started

任何关于为什么我得到 404 错误的帮助,我们非常感谢这个警告。我要再次强调它正在 Tomcat 上运行。提前致谢。

【问题讨论】:

  • 部署和启动应用程序时的上下文是否正确?
  • 你解决了这个问题吗?
  • 不,使用注解还是有问题。但它可以按预期使用 xml。
  • 您是采用 xml 方式还是找到了解决方法?

标签: spring-mvc jboss spring-java-config


【解决方案1】:

正在阅读 SivaLabs 的教程,在 JBoss 上运行与 tomcat 配合良好的应用程序时出现了这样的问题。通过将 DispatcherServlet 映射更改为 "/app/*" 来解决问题。您也可以尝试实现 WebApplicationInitializer 而不是使用抽象类。这是一个例子:

 @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/"); // i have a more or less big website, and can't see advantages by using "/*" mapping, this can be also a problem. 
    }

    private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocations("ua.company.config.WebConfig", "ua.company.config.PersistenceConfig", "ua.company.config.SecurityConfig");
        return context;
    }

【讨论】:

    【解决方案2】:

    您可能遇到Red Hat bug 1094248,“没有 web.xml 就无法覆盖默认 servlet”。此问题显然会影响 EAP 6.2、6.3 和 6.4.0。来自错误报告:

    将 Spring 调度程序 servlet 映射到 url 模式“/”不起作用 以编程方式。换句话说,覆盖默认 servlet 不是 可以使用 Java 代码。

    后果:许多用户将 Spring 的 DispatcherServlet 映射到 '/',并且 这在 web.xml 中完成时工作正常。然而当这个配置 以编程方式完成,默认 servlet 首先绑定到“/”。这个 防止以后绑定 DispatcherServlet。弹簧控制器不是 映射并检索到 404。

    解决方法(如果有):使用 web.xml 配置或地图调度程序 servlet 以编程方式转换为某些特定的 URL 模式。例如 "/dispatcher/*"

    对于 EAP 6.4,此问题已在版本 6.4.1 中得到修复。我不知道早期版本的 EAP。该错误已于 2017 年 1 月修复,因此您需要查找之后发布的补丁。

    有权访问红帽解决方案知识库的人可能还想查看solution 1211203

    【讨论】:

      猜你喜欢
      • 2014-12-12
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      • 2019-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多