在 JBoss 上使用 Spring MVC Java Config 出现 404 错误 [英] 404 error using Spring MVC Java Config on JBoss

查看:37
本文介绍了在 JBoss 上使用 Spring MVC Java Config 出现 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote a small Spring MVC application with Java Config. It is working perfectly fine on Tomcat but not on JBoss EAP 6.2. It gets successfully deployed on JBoss but I get this warning when I request any page defined by Spring MVC and 404 error in browser.

WARN [org.springframework.web.servlet.PageNotFound] (http-/127.0.0.1:8080-1) 未找到具有 URI [/example-web/pages/login.jsp] 的 HTTP 请求的映射在 DispatcherServlet 中名为dispatcher"

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

Here you can see my code:

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 配置:

Here is my Spring MVC configuration:

@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:

And RootConfig:

@Configuration
@ComponentScan
public class RootConfiguration {
}

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

During deployment, I can see in the log that the requests do get mapped:

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 上工作.提前致谢.

Any help about why I get 404 error and this warning is highly appreciated. Once again I should emphasize that it is working on Tomcat. Thanks in advance.

推荐答案

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

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:

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

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

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

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.

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

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

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

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.

有权访问 Red Hat 解决方案知识库的人可能还想查看解决方案 1211203.

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

这篇关于在 JBoss 上使用 Spring MVC Java Config 出现 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆