Follow these steps to create a standalone application with Jersey and Tomcat:
Adding Maven dependencies
Add the following dependencies to your pom.xml:
<properties>
<tomcat.version>8.5.4</tomcat.version>
<jersey.version>2.23.2</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
Adding Maven plugins
Add the following plugins to your pom.xml:
<build>
<finalName>tomcat-embedded-sample</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<finalName>tomcat-embedded-sample-${project.version}</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.launch.Launcher</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Creating a launcher class
Define a launcher class as following:
public class Launcher {
public static void main(String[] args) throws Exception {
new Launcher().start();
}
public void start() throws Exception {
String contextPath = "";
String appBase = ".";
String port = System.getenv("PORT");
if (port == null || port.isEmpty()) {
port = "8080";
}
Tomcat tomcat = new Tomcat();
tomcat.setPort(Integer.valueOf(port));
tomcat.getHost().setAppBase(appBase);
Context context = tomcat.addWebapp(contextPath, appBase);
Tomcat.addServlet(context, "jersey-container-servlet",
new ServletContainer(resourceConfig()));
context.addServletMapping("/api/*", "jersey-container-servlet");
tomcat.start();
tomcat.getServer().await();
}
private ResourceConfig resourceConfig() {
return new JerseyConfiguration();
}
}
Defining Jersey configuration
Create a class to configure your Jersey application:
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
packages("com.example");
}
}
Creating resource classes
Define your resource class. The following is just an example:
@Path("hello")
public class HelloWorldResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response helloWorld() {
return Response.ok("Hello World").build();
}
}
Compiling and running the application
To compile and run the application, follow these steps:
- Open a command line window or terminal.
- Navigate to the root directory of the project, where the
pom.xml resides.
- Compile the project:
mvn clean compile.
- Package the application:
mvn package.
- Look in the target directory. You should see a file with the following or a similar name:
tomcat-embedded-sample-1.0-SNAPSHOT.jar.
- Change into the target directory.
- Execute the JAR:
java -jar tomcat-embedded-sample-1.0-SNAPSHOT.jar.
- The application is available at
http://localhost:8080/api/hello.