Rob Crocombe
Rob Crocombe

Reputation: 361

Getting 404 error with Jersey Tomcat service

I am getting a 404 page when trying to run a RESTful service in Java with Jersey and Tomcat.

Here is my project:

Here is the HelloWorld.java:

package service;
import javax.ws.rs.*;

@Path("/hello")
public class HelloWorld {
    @GET
    @Produces("text/plain")
    public String get() {
        return "Hello World";
    }
}

Here is the web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <display-name>EclipseTest</display-name>
 <servlet>
  <display-name>Rest Servlet</display-name>
  <servlet-name>RestServlet</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
   <param-name>jersey.config.server.provider.packages</param-name>
   <param-value>service</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>RestServlet</servlet-name>
  <url-pattern>/service/*</url-pattern>
 </servlet-mapping>
</web-app>

I have tried these URLs based on other SO answers:

http://localhost:8080/EclipseTest/service/hello

http://localhost:8080/service/hello

When using 'Run As > Run on Server' it sends me to this page (404s): http://localhost:13036/EclipseTest/WEB-INF/classes/service/HelloWorld.java

I am using:

Any help would be very appreciated.

Upvotes: 0

Views: 1466

Answers (2)

Your path is incorrect.

Your context path is EclipseTest so if you wanna access the helloworld then you should access like contextpath+path param

so it should be

localhost:8080/EclipseTest/hello

Because you have mapped the class to hello so contextpath + path -> EclipseTest/hello

Upvotes: 1

Safwan Hijazi
Safwan Hijazi

Reputation: 2089

change the code like this:

package service;
import javax.ws.rs.*;

@Path("/hello")
public class HelloWorld {
@GET
@Path("/get")
@Produces("text/plain")
public String get() {
    return "Hello World";
}
}

and call like this http://localhost:8080/EclipseTest/service/hello/get

Upvotes: 0

Related Questions