Issue
I am working on migrating the spring project into spring boot. While migrating, I am importing the existing applicationcontext.xml file in my main class to import the existing beans defined for spring project. I did not use any @ComponentScan
annotation. After doing this, my tomcat is up but while hitting the endpoint for health check, it gives an error 401 Error "This application has no explicit mapping for / error.
I have created a main class in a different package than the package where my api end points exist. trIied adding the @componentScan
annotation with the package for component scan then it gives an error "expecting a single bean but found 2". I am feeling I am missing some configuration to make it work.
The spring configuration had web.xml
file with servlets and servlet mapping but I removed it since web.xml is not required for spring boot application. So I am wondering if i need to address the servlet mapping in some way for spring boot so that the mapping will work as it was working in spring.
Any guidance will be highly appreciated. Below is my code snippet.
Application.java:
@SpringBootApplication
@ImportResource({"classpath*:applicationContext.xml"})
public class Application {
public static void main (String[] args) {
SpringApplication.run(Application.class, args);
}
}
Web.xml:
<web-app
xmlns: xsi="http://www.w3.org/2001/XML Schema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3.0.xsd"
version="3.0">
<display-name>Servelt 3.0 Web Application </display-name>
<session-config>
<cookie-config>
<http-only>true</http-only>
<secure>true</true>
<cookie-config>
</session-config>
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer<servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.api.AppResourceConfig</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.wadl.disablewadl</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>jerserysecure</servlet-name> ServletCOntainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.secureapi.AppSecureResourceConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/api/*<url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jerseysecure</servlet-name>
<url-pattern>/secureapi/*<url-pattern>
</servlet-mapping>
</web-app>
AppResourceConfig.java:
public class AppResourceConfig extends ResourceConfig {
public AppResourceConfig {}{
super();
property("jersery.config.beanValidation.enableOutputValidationErrorEntity.server");
packages("com.api");
register(GsonProvider.class);
register(RequestContextFilter.class);
register(NotFoundExceptionMapper.class);
register(DefaultExceptionMapper.class);
}
}
AppSecureResourceConfig.java
public class AppSecureResourceConfig extends ResourceConfig {
public AppSecureResourceConfig{}{
super();
property("jersery.config.beanValidation.enableOutputValidationErrorEntity.server");
packages("com.secureapi");
register(GsonProvider.class);
register(RequestContextFilter.class);
register(NotFoundExceptionMapper.class);
register(DefaultExceptionMapper.class);
}
}
Below is the url used for health check: localhost:8080/rest-api-web/api/health
Solution
I am able to resolve this issue with the below steps:
- Removed all the jersey dependencies from pom.xml file and added spring boot starter dependency as below.
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
</dependencies>
- In my Application.java class, I added a bean with annotation @bean for the servlet.
Application.java:
@Bean
public ServletRegistrationBean<HttpServlet> dispatcherApiServlet() {
ServletRegistrationBean<HttpServlet> servRegBean = new
ServletRegistrationBean<HttpServlet>();
servRegBean.setServlet(new ServletContainer());
servRegBean.addUrlMappings("/api/*");
servRegBean.addInitParameter("javax.ws.rs.Application",
"com.api.AppResourceConfig ");
servRegBean.addInitParameter("jersey.config.server.wadl.disableWadl",
"true");
servRegBean.setLoadOnStartup(1);
return servRegBean;
}
- Optional: you can mark the config class with @configuration annotation but this is not mandatory
Answered By - Suraj
Answer Checked By - Terry (JavaFixing Volunteer)