Search code examples
javamavenjersey-2.0maven-jetty-plugin

basic Jersey webservice with maven jetty plugin


I have been working with the restfull webservices provide by Jersey in combination with the maven jetty plugin at my previous job. But now for a personal project i am starting from scratch by myself and i cant seem to get even the basic version going. I have tried to follow a few tutorial but they are either old or off topic.

The most recent example i tried was: jersey 2 + spring 4 + jetty-maven-plugin

So the below configs make the plug in run but no matter which url i try it gives me 404's

pom:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <groupId>foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <jetty.version>9.3.11.v20160721</jetty.version>
        <jersey.version>2.23.1</jersey.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.version}</version>
            </plugin>
        </plugins>
    </build>
</project>

web.xml

<?xml version="1.0"  encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>test</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.foo.bar</param-value>
        </init-param>
        <!--<load-on-startup>1</load-on-startup>-->
    </servlet>

    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

resourceConfiguration.java

package com.foo.bar;

import org.glassfish.jersey.server.ResourceConfig;

public class ResourceConfiguration extends ResourceConfig {
    public ResourceConfiguration() {
        register(entrypoint.class);
    }

}

and my service class

package com.foo.bar;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

public class entrypoint {
    @GET
    @Path("/test")
    @Produces(MediaType.TEXT_PLAIN)
    public String test() {
        return "it works";
    }

    @GET
    @Path("/")
    @Produces(MediaType.TEXT_HTML)
    public String test2() {
        return "it works";
    }

}

Nothing fancy right i feel like this should work out of the box. The Jetty server starts but like i said i get jetty 404 pages at every url permutation i try.

Thanks in advance for any and all input.


Solution

  • Few Changes need to be done to make it work. please follow the below steps.

    Step 1: change your web.xml like below with the POJO mapping and Resourse configuration class instead of package name.

    <servlet>
            <servlet-name>Jersey REST Service</servlet-name>
            <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
            <init-param>
                <param-name>javax.ws.rs.Application</param-name>
                <param-value>bar.com.ravi.ResourceConfiguration</param-value>
            </init-param>
           <init-param>
                <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
                <param-value>true</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>Jersey REST Service</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    

    Step 2: change your ResourceConfiguration class like below with package information.

    public class ResourceConfiguration extends ResourceConfig {
        public ResourceConfiguration() {
            packages("bar.com.ravi");
            register(Entrypoint.class);
        }
    
    }
    

    Step 3: Change your EntryPoint class with Class level @Path Annotation.

      import javax.ws.rs.GET;
        import javax.ws.rs.Path;
        import javax.ws.rs.Produces;
        import javax.ws.rs.core.MediaType;
    
        @Path("/hi")
        public class Entrypoint {
            @GET
            @Path("/test")
            @Produces(MediaType.TEXT_PLAIN)
            public String test() {
                return "it works";
            }
    
            @GET
            @Path("/")
            @Produces(MediaType.TEXT_HTML)
            public String test2() {
                return "it works";
            }
    
        }
    

    Step 4: Change your POM.xml in build tag like below.

    <build>
        <finalName>bar</finalName>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-maven-plugin</artifactId>
                    <version>9.2.11.v20150529</version>
                    <configuration>
                        <scanIntervalSeconds>10</scanIntervalSeconds>
                        <webApp>
                            <contextPath>/bar</contextPath>
                        </webApp>
                        <httpConnector>
                            <port>8888</port>
                        </httpConnector>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    Step 5: run below command your command promt.

    mvn jetty:run
    

    Step 6: hit the browser with below URL

    http://localhost:8888/bar/hi/test