Reputation: 1552
I am using Jersey, Maven; and could use Jetty, Tomcat or J2EE Preview (is that embeddable?).
Upvotes: 3
Views: 8327
Reputation: 130987
Follow these steps to create a standalone application with Jersey and Jetty:
Add the following dependencies and properties to your pom.xml
:
<properties>
<jetty.version>9.4.7.v20170914</jetty.version>
<jersey.version>2.26</jersey.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
Define your JAX-RS resource class(es). 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();
}
}
Create a class to configure your Jersey application:
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
packages("com.example");
}
}
Create a class to launch Jetty and deployment your application:
public class Launcher {
private static final String JERSEY_SERVLET_NAME = "jersey-container-servlet";
public static void main(String[] args) throws Exception {
new Launcher().start();
}
void start() throws Exception {
String port = System.getenv("PORT");
if (port == null || port.isEmpty()) {
port = "8080";
}
Server server = new Server(Integer.valueOf(port));
ServletContextHandler context = new ServletContextHandler(server, "/");
ServletHolder servlet = new ServletHolder(JERSEY_SERVLET_NAME,
new ServletContainer(new JerseyConfiguration()));
context.addServlet(servlet, "/api/*");
try {
server.start();
server.join();
} finally {
server.destroy();
}
}
}
Finally add the Maven Shade plugin to create an executable JAR, where the mainClass
attribute references the launch class:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<finalName>jetty-embedded-example-${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.Launcher</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
To compile and run the application, follow these steps:
pom.xml
resides.mvn clean compile
.mvn package
.jetty-embedded-example-1.0-SNAPSHOT.jar
.java -jar jetty-embedded-example-1.0-SNAPSHOT.jar
.http://localhost:8080/api/hello
.Upvotes: 6
Reputation: 130987
Follow these steps to create a standalone application with Jersey and Tomcat:
Add the following dependencies and properties to your pom.xml
:
<properties>
<tomcat.version>8.5.23</tomcat.version>
<jersey.version>2.26</jersey.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<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.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
Define your JAX-RS resource class(es). 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();
}
}
Create a class to configure your Jersey application:
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
packages("com.example");
}
}
Create a class to launch Tomcat and deployment your application:
public class Launcher {
private static final String JERSEY_SERVLET_NAME = "jersey-container-servlet";
public static void main(String[] args) throws Exception {
new Launcher().start();
}
void start() throws Exception {
String port = System.getenv("PORT");
if (port == null || port.isEmpty()) {
port = "8080";
}
String contextPath = "";
String appBase = ".";
Tomcat tomcat = new Tomcat();
tomcat.setPort(Integer.valueOf(port));
tomcat.getHost().setAppBase(appBase);
Context context = tomcat.addContext(contextPath, appBase);
Tomcat.addServlet(context, JERSEY_SERVLET_NAME,
new ServletContainer(new JerseyConfiguration()));
context.addServletMappingDecoded("/api/*", JERSEY_SERVLET_NAME);
tomcat.start();
tomcat.getServer().await();
}
}
Finally add the Maven Shade plugin to create an executable JAR, where the mainClass
attribute references the launch class:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<finalName>tomcat-embedded-example-${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.Launcher</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
To compile and run the application, follow these steps:
pom.xml
resides.mvn clean compile
.mvn package
.tomcat-embedded-example-1.0-SNAPSHOT.jar
.java -jar tomcat-embedded-example-1.0-SNAPSHOT.jar
.http://localhost:8080/api/hello
.Upvotes: 11