Search code examples
javaservletsjettymaven-3jersey-2.0

jetty+jersey servlet mapping not working


i am trying to implement a REST web service using jetty and jersey. I created a maven webapp project and i also want to use the maven jetty plugin in this project.

The problem i am facing right now is that the servlet-mapping does not seem to work. I want the resource being mapped to http://localhost:8080/rest/test which i tried in the web.xml but when accessing the URI i get 404 not found.

Here is the pom.xml:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.testproject</groupId>
  <artifactId>rest</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>rest Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.22.1</version>
</dependency>

    <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.22.1</version>
</dependency>

    <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.22.1</version>
</dependency>

    <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-jetty-http</artifactId>
    <version>2.22.1</version>
</dependency>  
    <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-jetty-servlet</artifactId>
    <version>2.22.1</version>
</dependency>


  </dependencies>

  <build>

  <plugins>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.3.6.v20151106</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>          
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>

      </plugins>
    <finalName>rest</finalName>
  </build>
</project>

RestResource.java

package com.testproject.rest.resource;

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

@Path("/rest")
public class RestResource {

@GET
@Path("/test")
@Produces(MediaType.TEXT_PLAIN)
public String testService(){
    return "success!";
}

}

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>


    <servlet>
        <servlet-name>REST</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.testproject.rest.resource</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>REST</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>


</web-app>

I did search for this problem in google but none of the solutions i found seem to work. And since this is my first time working with web servers and web services, i dont't entirely understand the whole topic yet.


Solution

  • Change @Path("/rest") to @Path("/test"), and get rid of the other @Path("/test") on the method. Every method not annotated with @Path, will go to /test on the class.

    /rest/* is already define in the servlet mapping in the web.xml, so that will be be the root.

    How you currently have it, you would need to access. /rest/rest/test