Spring Boot application doesn't work when deployed on JBoss EAP
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 6.x
Issue
- Spring Boot application doesn't work when deployed on JBoss EAP
- Spring
@RestControllerreturns page not found, 404, when using Spring Boot
Resolution
The [JBoss Web bug](This content is not included.bug 1094248) was fixed in EAP 6.4 Update 1, apply the latest/last Update 24 is recommended.
EAP 6.4 is in ELS-2 , there are no more updates coming, it is recommended to upgrade to a current version of EAP.
As a work-around:
- If no applications need access to static resources, disable them in the JBoss web subsystem:
<subsystem xmlns="urn:jboss:domain:web:1.5" ...>
<configuration>
<static-resources disabled="true"/>
</configuration>
...
- If the application is simple enough, manually configure the Spring dispatcher in web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>demo</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Remember to change contextConfigLocation to point to the application's package that contains the Spring configuration classes.
Spring boot application - EAR
When spring boot is packaged as EAR there are some additional changes necessary. See attached example, which can be used like this:
$ mvn clean package
$ ./jboss-cli.sh
$ deploy web-all/target/web-all.ear
This EAR application was tested with JBoss EAP 7.0.x
Root Cause
There is a This content is not included.bug 1094248 in EAP 6 related to JSR-315 Servlet 3.0 specification (http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/) section 12.2, where the url pattern "/" should override default servlet mapping, but due to the bug, an application configured this way returns 404.
The bug was an issue related to Servlets, it just happened a user using Spring/Spring Boot hit the issue due to Spring trying to override the default servlet mapping.
Diagnostic Steps
On JBoss CLI, verify if Spring's DispatcherServlet is being registered.
Not working:
[standalone@localhost:9999 /] /deployment=spring-boot-application.war/subsystem=web:read-resource(recursive=true)
{
"outcome" => "success",
"result" => {
"context-root" => "/spring-boot-application",
"servlet" => undefined,
"virtual-host" => "default-host"
}
}
Working:
[standalone@localhost:9999 /] /deployment=spring-boot-application.war/subsystem=web:read-resource(recursive=true)
{
"outcome" => "success",
"result" => {
"context-root" => "/spring-boot-application",
"virtual-host" => "default-host",
"servlet" => {"appServlet" => {
"servlet-class" => "org.springframework.web.servlet.DispatcherServlet",
"servlet-name" => "appServlet"
}}
}
}
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.