Java Rest Intro - JAR

 

https://stackoverflow.com/questions/39343021/how-to-turn-my-jersey-rest-api-into-an-executable-jar

 

How to turn my Jersey REST API into an executable JAR?

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

  1. What is the easiest way to port my REST API as a standalone/executable JAR?
  2. Can I do it without Spring Boot?
share|improve this question
 
    
What is J2ee Preview? – Cássio Mazzochi Molin Sep 6 '16 at 8:00
    
Eclipse provided lightweight server. – Raúl Sep 6 '16 at 8:01
    
It could be achieved with Spring Boot and Tomcat embedded. – Cássio Mazzochi Molin Sep 6 '16 at 8:04
1  
Thank you. How about without Spring boot? – Raúl Sep 6 '16 at 8:05

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.
share|improve this answer
 
    
I thought 'maven-shade-plugin' would be required in the <build> section of pom. Isn't it? – Raúl Sep 6 '16 at 10:59
    
@Raúl I don't think so. I managed to run it without any additional plugins. – Cássio Mazzochi Molin Sep 6 '16 at 11:02
1  
Don't use the assembly plugin. It will leave out required META/services files. Instead use the shade plugin that will concatenate these files. See this post – peeskillet Sep 6 '16 at 11:46
    
@peeskillet That's an interesting advice. Thanks for pointing it. – Cássio Mazzochi Molin Sep 6 '16 at 11:51

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

Adding Maven dependencies

Add the following dependencies to your pom.xml:

<properties>
    <jetty.version>9.3.11.v20160721</jetty.version>
    <jersey.version>2.23.2</jersey.version>
    <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-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>
</dependencies>

Adding Maven plugins

Add the following plugins to your pom.xml:

<build>
    <finalName>jetty-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>jetty-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 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(new ServletContainer(resourceConfig()));
        context.addServlet(servlet, "/api/*");

        try {
            server.start();
            server.join();
        } finally {
            server.destroy();
        }
    }

    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: jetty-embedded-sample-1.0-SNAPSHOT.jar.
  • Change into the target directory.
  • Execute the JAR: java -jar jetty-embedded-sample-1.0-SNAPSHOT.jar.
  • The application is available at http://localhost:8080/api/hello.

posted on 2017-09-20 16:38  fanbird2008  阅读(128)  评论(0)    收藏  举报

导航