I am missing something obvious, but I’m not sure what. I have a single “HelloWorld.java” that has a single @GET method that returns some text.
My web.xml was taken from this doc (described as “An even simpler approach is to let Jersey choose the PackagesResourceConfig implementation automatically….”):
<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.hello.rest</param-value>
</init-param>
</servlet>
</web-app>
Here’s my class (mostly taken from here):
package com.hello.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
@Path("/helloworld")
public class HelloWorld {
@GET
@Produces("text/json")
public String getHelloWorld() {
return "{\"hello\":\"World\"}";
}
}
I use ant to build a war file, and deploy it to tomcat. The war appears correct because tomcat unzips it and I can access my static index.html that I put in it for testing. But accessing localhost:8080/helloworld gives me a 404. There must be some other piece I need in order to get Jersey working. What did I miss?
Thank you Bozho, I was missing the <servlet-mapping> section. Actually it appears I don’t want a “/” as url-pattern, because that prevents serving static content (I can’t get my index.html page any more!) so here’s my new web.xml (I put my resource in the “/data/” path):
<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.hello.rest</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/data/*</url-pattern>
</servlet-mapping>
</web-app>
Now I can access my index.html page as http://localhost:8080/hello/index.html, and my resource at http://localhost:8080/hello/data/helloworld.
You have to map your servlet with
<servlet-mapping>, with a/asurl-pattern