Spring Java Config vs Jboss 7
I had a similar problem with a Spring MVC project deployed to JBoss 7.1 with no web.xml.
According to Spring javadocs for WebApplicationInitializer, older versions of Tomcat (<=7.0.14) could not be mapped to "/" programmatically. Older versions of JBoss AS 7 have this same defect.
This was the source of my problem. I was registering the servlet via "/", but JBoss EAP 6.4 doesn't support this mapping programmatically. It only works via web.xml. I still wanted to use programmatic config, so I changed the mapping to "/*" instead of "/", and it fixed my issue.
public class WebApplicationInitializerImpl implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
WebApplicationContext context = getContext();
Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(context));
registration.setLoadOnStartup(1);
registration.addMapping("/*");
}
private WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(AppConfig.class.getName());
return context;
}
}
Note: This configuration is incompatible with JSP views. "/*" will supersede the servlet container's JSP Servlet. If you still rely on JSP views, I would recommend using web.xml to configure the DispatcherServlet instead of doing it programmatically; the web.xml configuration works with "/" correctly.
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I was using @SpringBootApplication
As I read in this thread I needed to:
Change the mapping of the DispatcherServlet to "/*" instead of "/" (by adding a @Bean of type ServletRegistrationBean with a servlet named "dispatcherServlet")
In this url I found the code solution: Add Servlet Mapping to dispatch servlet
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
/**
* Register dispatcherServlet programmatically
*
* @return ServletRegistrationBean
*/
@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet(), "/*");
registration
.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
return registration;
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}