I`m trying to run a simple application with spring java based configuration on jboss, but no success. This application works fine both on jetty and tomcat. The jboss log loo
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);
}
}
I had similar problems with JBoss 6.4.0. and Spring Boot 1.3 in combination with Tiles 3. After installing the Jboss patch jboss-eap-6.4.6-patch.zip the problems were solved.
After patching I did not need to use the setting server.servlet-path=/*
We have a spring-boot (1.1.4) project on JBoss EAP 6.2 (my customer's requirement...)
I found a solution to run it on JBoss EAP 6.2.0 GA and keep capability to run on Apache Tomcat 7 container.
Initially my project run in embedded mode, so I need to create and change some files to run on containers.
To run on Tomcat as root application I created context.xml: /src/main/webapp/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path=""/>
To run on JBOSS EAP 6.2.0 GA as root application I created jboss-web.xml: /src/main/webapp/WEB-INF/jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<context-root>/</context-root>
</jboss-web>
I created a class, because JBoss servlet mapping works as /* but not with / :
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
/**
* Working without web.xml with container (not em,bedded mode).
* JBOSS EAP 6.2 specific: you need to map dispatcherServlet to /* .
*/
public class ContainerWebXml extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(TomcatStart.class);
}
/**
* JBOSS EAP 6.2 mapping.
*
* @param container
* @throws ServletException
*/
@Override
public void onStartup(ServletContext container) throws ServletException {
WebApplicationContext context = getContext();
Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(context));
registration.setLoadOnStartup(1);
registration.addMapping("/*"); // required JBOSS EAP 6.2.0 GA
super.onStartup(container);
}
private WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(TomcatStart.class.getName());
return context;
}
}
Do not forget call super.onStartup(container);
Changes in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
If you use spring.profile, than you need to set as env. variable.
I run JBoss EAP 6.2.0 GA as standalone mode:
export JAVA_OPTS="-Dspring.profiles.active=local"
.../jboss-eap-6.2/bin/standalone.sh
If you run on Tomcat, then do not forget to set -Dspring.profiles.active=local
As I see the server.port setting will be ignored when you run on container.
I'm using Spring Boot 1.3.1 and JBoss EAP 6.4. And I found with that in your project you can add to src/main/resources/application.properties this line:
server.servlet-path=/*
Also, if you're launching this out of Eclipse, be sure to clean your project... I blew a lot of time because of that alone.
I am using JBoss EAP 6.4. I was going through the thread.
I would like to add that after changing mapping for dispatchServlet from "/" to "/* ". The JSP in your project might not process correctly. I suspect that since "/* " have more precedence over "/*.jsp ", therefore, JSPServlet might not be getting request to process the JSP and JSP will not be processed correctly. In my case, source of JSP is coming on browser as text.
I have resolved this issue by defining JSP as servlet in web.xml as mentioned below. After that things worked fine for me :)
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<jsp-file>/login.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>IndexServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<!--mapping -->
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>IndexServlet</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
I spent lot of time on this, might help someone :))