I am scaling back a large web application that included a web service to become only a Jersey web service, on Spring Boot 1.5.2. Because the web service already had a complete set of JAX-RS annotations implemented by Apache Wink, I decided to go with Spring + Jersey instead of Spring Rest. I found this spring-boot-jersey-sample application to use as a reference. The biggest difference between the application I'm working on and the sample is that my endpoint definitions are divided between interfaces and implementations.
I added the following to my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
My new Jersey Config looks like this:
package com.example.configuration;
import org.glassfish.jersey.server.ResourceConfig;
import com.example.EndpointImpl;
import org.springframework.stereotype.Component;
@Component
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
registerEndpoints();
}
private void registerEndpoints() {
register(EndpointImpl.class);
}
}
Then I have the following Application.java:
package com.example;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
new Application().configure(new SpringApplicationBuilder(Application.class)).run(args);
}
}
The endpoints are defined as an interface and an implementation, like this (minus imports):
public interface Endpoint {
@GET
@Produces({MediaType.APPLICATION_JSON})
public Response getHello(@Context ServletContext sc, @Context HttpServletRequest req, @Context HttpHeaders httpHeaders) ;
}
@Path("")
@Component
public class EndpointImpl implements Endpoint {
@Override
public Response getHello(@Context ServletContext sc, @Context HttpServletRequest req,
@Context HttpHeaders httpHeaders) {
return Response.ok("hello").build();
}
}
When I start up my application, I see messages saying Tomcat has started up, including a messges saying Mapping servlet: 'com.example.configuration.JerseyConfiguration' to [/*]
. However, when I go to / with a web browser, I get a 404 Not Found
error. It doesn't look like the GET definition is getting picked up.