如何将我的 Jersey REST API 变成可执行的 JAR?我的、可执行、如何将、REST

2023-09-08 08:32:54 作者:他似梦别当真

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

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

将我的 REST API 移植为独立/可执行 JAR 的最简单方法是什么?不使用 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 配置类

ES Rest API操作

创建一个类来配置您的 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 属性引用启动类:

<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. 如何在 Java SE 环境中部署 JAX-RS 应用程序?