3
votes

I'm trying to get a simple REST-Service running with Jersey 2 and Tomcat 6 using maven. IntelliJ says my app was successfully deployed and I can access my index.jsp file. When trying to navigate to my rest endpoint I always get a 404 error message:

http://localhost:8080/rest/hello

http://localhost:8080/HelloWorld/rest/hello

web.xml:

<servlet>
    <description>JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>JAX-RS Servlet</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.tutorial.example</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

pom.xml:

<groupId>com.tutorial.example</groupId>
<artifactId>HelloWorld</artifactId>
<version>1.0-SNAPSHOT</version>


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

HelloWorld.java:

package com.tutorial;

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

@Path("/hello")
public class HelloWorld {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

enter image description here

Is something wrong with my URL? There are no errors in the tomcat log.

UPDATE:

It worked once I added <packaging>war</packaging> in my pom.xml and accessed the REST-endpoint via "http://localhost:8080/rest/hello".

1

1 Answers

2
votes

<param-value>com.tutorial.example</param-value>

Your class is not in the com.tutorial.example package. It's in the com.tutorial package. Jersey scans the package you put there for classes, and registers them. In your case, do classes get picked up. Make the change accordingly.

Note that the package you put, will be scanned recursively. Also you can put multiple packages separated by a comma.