404 error using Spring MVC Java Config on JBoss

前提是你 提交于 2019-12-01 08:37:13

Was reading tutorial from SivaLabs, there was such a problem with running application on JBoss that work fine with tomcat. Problem was solving by changing DispatcherServlet mapping to "/app/*" . You can also try implementing WebApplicationInitializer instead of using abstract class. Here is example:

 @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;
    }

It's possible that you were experiencing Red Hat bug 1094248, "Default servlet can't be overridden without web.xml". This issue apparently affects EAP 6.2, 6.3, and 6.4.0. From the bug report:

Mapping Spring dispatcher servlet to url pattern "/" does not work programmatically. In other words, overriding default servlet is not possible with Java code.

Consequence: Many users map Spring's DispatcherServlet to '/', and this works fine when done in web.xml. However when this configuration is done programmatically, default servlet is bind to "/" first. This prevents to bind DispatcherServlet later. Spring controllers are not mapped and 404 is retrieved.

Workaround (if any): Use web.xml configuration or map dispatcher servlet programmaticaly to some specific URL pattern. e.g. "/dispatcher/*"

For EAP 6.4, this was fixed in version 6.4.1. I don't know about earlier versions of EAP. The bug was fixed in January 2017, so you'd want to look for patches issued after then.

People with access to Red Hat's solutions knowledgebase may also want to look at solution 1211203.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!