There are other people asking this question, but the answers they got aren't working for me. I'm running a Jersey rest server as discussed in this link in Tomcat 7. My Resource class is Hello as shown below:
package com.rest.videos;
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 Hello {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
My web.xml file is the following:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Videos</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletAdaptor</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.rest.videos</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Whenever I visit the URL http://localhost:8080/Videos/rest/hello
, it returns 404. I have an index.html file in my WEB-INF directory and if I go to http://localhost:8080/Videos/
where I assume it should be since the welcome-file-list says so, I also get a 404.
What am I doing wrong?
Ok, I'll post an answer then if you don't mind giving credits :)
web.xml has not bearing on the URI path, so unless your WAR is wrapped into an EAR w/ a of /Videos, this prefix is invalid and likely is the root of your 404s.
Based on the tutorial you referred to, it seems you should try the following:
http://localhost:8080/com.rest.videos/rest/hello
Notice the base URI the client code is using?
servlet-mapping
inweb.xml
@Path
annotation