Search code examples
javajerseyjetty

Simple Jersey service running on Jetty 11


Been fighting this for days and trying to get a simple rest service started up on Jetty 11. I see the war deployed, but doesn't seem like the endpoint is active. I can't even get a breakpoint to hit in IntelliJ. Help?

Requests to http://localhost:8080/jakartaee-sample-1.0-SNAPSHOT/api/hello-world fail with a 404

To deploy, I am using JRE 11, Jetty 11.0.3, with modules jmx, http, deploy, tsp, server. And launching via IntelliJ. No errors in the Output.

HelloApplication.java

import jakarta.ws.rs.ApplicationPath;

@ApplicationPath("/api") public class HelloApplication {
    public HelloApplication() {
        System.console().printf("testing");
    } }

HelloResource.java

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;

@Path("/hello-world")
public class HelloResource {
    @GET
    @Produces("text/plain")
    public String hello() {
        return "Hello, World!";
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>jakartaee-sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>jakartaee-sample</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-http</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Solution

  • Changes you need to make.

    First, the application declaration needs fixing ...

    import jakarta.ws.rs.ApplicationPath;
    
    @ApplicationPath("/api") 
    public class HelloApplication {
    

    ... to ...

    import jakarta.ws.rs.ApplicationPath;
    import jakarta.ws.rs.core.Application; // new import
    
    @ApplicationPath("api") // change this line
    public class HelloApplication extends Application { // and this line
    

    Next, your dependencies in your pom. You'll want the servlet container generic for working with <packaging>war</packaging> and you'll want to flag the servlet api as <scope>provided</scope>.

      <dependencies>
        <dependency>
          <groupId>org.glassfish.jersey.containers</groupId>
          <artifactId>jersey-container-servlet</artifactId> <!-- changed -->
          <version>3.0.2</version>
        </dependency>
        <dependency>
          <groupId>org.glassfish.jersey.inject</groupId>
          <artifactId>jersey-hk2</artifactId>
          <version>3.0.2</version>
        </dependency>
        <dependency>
          <groupId>jakarta.servlet</groupId>
          <artifactId>jakarta.servlet-api</artifactId>
          <version>5.0.0</version>
          <scope>provided</scope> <!-- changed -->
        </dependency>
      </dependencies>
    

    Finally, make sure your ${jetty.base} has the "annotations" module enabled.

    $ cd /path/to/my-jetty-base
    $ java -jar /path/to/jetty-home/start.jar --add-module=annotations
    $ cp /path/to/my-code/example-ws.war webapps/
    $ java -jar /path/to/jetty-home/start.jar