如何将Jersey REST API转换为可执行JAR? [英] How to turn my Jersey REST API into an executable JAR?

查看:139
本文介绍了如何将Jersey REST API转换为可执行JAR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用泽西岛,Maven;并可以使用Jetty,Tomcat或J2EE Preview(可嵌入吗?).

I am using Jersey, Maven; and could use Jetty, Tomcat or J2EE Preview (is that embeddable?).

  1. 将REST API移植为独立/可执行JAR的最简单方法是什么?
  2. 在没有Spring Boot的情况下可以做到吗?

推荐答案

请按照以下步骤使用 Jersey和Tomcat 创建独立的应用程序:

Follow these steps to create a standalone application with Jersey and Tomcat:

将以下依赖项和属性添加到您的pom.xml:

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>

创建JAX-RS资源类

定义您的JAX-RS资源类.以下仅是示例:

Creating JAX-RS resource classes

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();
    }
}

创建Jersey配置类

创建一个类来配置您的Jersey应用程序:

Creating a Jersey configuration class

Create a class to configure your Jersey application:

public class JerseyConfiguration extends ResourceConfig {

    public JerseyConfiguration() {
        packages("com.example");
    }
}

为Tomcat创建启动器类

创建一个类以启动Tomcat并部署您的应用程序:

Creating a launcher class for Tomcat

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();
    }
}

添加用于创建可执行JAR的Maven插件

最后添加Maven Shade插件以创建可执行的JAR,其中mainClass属性引用启动类:

Adding Maven plugin for creating an executable JAR

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>

编译并运行应用程序

要编译和运行该应用程序,请按照下列步骤操作:

Compiling and running the application

To compile and run the application, follow these steps:

  • 打开命令行窗口或终端.
  • 导航至项目pom.xml所在的根目录.
  • 编译项​​目:mvn clean compile.
  • 打包应用程序:mvn package.
  • 查找目标目录.您应该看到一个具有以下名称或类似名称的文件:tomcat-embedded-example-1.0-SNAPSHOT.jar.
  • 切换到目标目录.
  • 执行JAR:java -jar tomcat-embedded-example-1.0-SNAPSHOT.jar.
  • 该应用程序应该在http://localhost:8080/api/hello处可用.
  • 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-example-1.0-SNAPSHOT.jar.
  • Change into the target directory.
  • Execute the JAR: java -jar tomcat-embedded-example-1.0-SNAPSHOT.jar.
  • The application should be available at http://localhost:8080/api/hello.

这篇关于如何将Jersey REST API转换为可执行JAR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆