Skip to content

Instantly share code, notes, and snippets.

@theotherian
Last active September 24, 2017 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theotherian/6181277 to your computer and use it in GitHub Desktop.
Save theotherian/6181277 to your computer and use it in GitHub Desktop.
There are two dependencies that Jersey 2.0 has for adapting its application to a Servlet instance.
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("hello")
public class HelloResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response get() {
return Response.ok("hello").build();
}
}
<!-- use the following artifactId for servlet 3.x configuration features that don't require web.xml -->
<!-- This supports Servlet 2.x -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.1</version>
</dependency>
<!-- This does NOT support Servlet 2.x -->
<!-- (but it does support Servlet 3.0's web.xml-less configuration) -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.1</version>
</dependency>
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("myapp")
public class JerseyApp extends ResourceConfig {
public JerseyApp() {
register(HelloResource.class);
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.theotherian</groupId>
<artifactId>jersey-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>jersey-example</name>
<build>
<finalName>jersey-example</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- uncomment this line and comment out jersey-container-servlet to see this break -->
<!-- <artifactId>jersey-container-servlet-core</artifactId> -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</project>
@sartan
Copy link

sartan commented Feb 27, 2014

Thank you, thank you, THANK YOU!

I spent 3hrs trying to get Servlet 3.x + Jersey 2.x to play nice together. Nothing but 404s, until I found your blog post (http://www.theotherian.com/2013/08/jersey-2.0-servlet-3.0-problems-applicationpath.html) on jersey-container-servlet-core vs jersey-container-servlet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment