我正在使用Jersey,Maven;并且可以使用Jetty,Tomcat或J2EE Preview(可嵌入?)。
答案 0 :(得分:6)
按照以下步骤使用 Jersey和Tomcat 创建独立应用程序:
将以下依赖项和属性添加到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>
定义您的JAX-RS资源类。以下只是一个例子:
@Path("hello")
public class HelloWorldResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response helloWorld() {
return Response.ok("Hello World").build();
}
}
创建一个类来配置Jersey应用程序:
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
packages("com.example");
}
}
创建一个类来启动Tomcat并部署您的应用程序:
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();
}
}
最后添加Maven Shade插件以创建可执行JAR,其中mainClass
属性引用启动类:
<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>
要编译并运行该应用程序,请按以下步骤操作:
pom.xml
所在的项目的根目录。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
。答案 1 :(得分:4)
按照以下步骤创建一个包含 Jersey和Jetty 的独立应用程序:
将以下依赖项和属性添加到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>
定义您的JAX-RS资源类。以下只是一个例子:
@Path("hello")
public class HelloWorldResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response helloWorld() {
return Response.ok("Hello World").build();
}
}
创建一个类来配置Jersey应用程序:
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
packages("com.example");
}
}
创建一个类来启动Jetty并部署您的应用程序:
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();
}
}
}
最后添加Maven Shade插件以创建可执行JAR,其中mainClass
属性引用启动类:
<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>
要编译并运行该应用程序,请按以下步骤操作:
pom.xml
所在的项目的根目录。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
。