1

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?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 1
    where is /Videos prefix configured? Is that WAR part of an EAR w/ a ? – TheArchitect Oct 10 '12 at 23:38
  • So my fault is that the Context root of the application was /Rest and so the proper url to use was `localhost:8080/Rest/rest/hello`. The `display-name` attribute in the web-xml has nothing to do with that. – Sotirios Delimanolis Oct 10 '12 at 23:39

2 Answers2

1

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.

TheArchitect
  • 2,161
  • 1
  • 12
  • 16
0

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?

private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080/de.vogella.jersey.first").build();
 }
  • com.rest.videos refers to the package name
  • /rest refers to the servlet-mapping in web.xml
  • /hello refers to the @Path annotation
Laurent
  • 107
  • 7
  • Kind of. I called my Project Rest instead of the name of the packaage as given in the tutorial. When you create a dynamic web project in eclipse, it sets the context root of the app to /the-name-of-the-app. So the uri in the tutoriaal doesn't refer to any mapping in the web.xml, but to the context root of the app. This works regardless of the package name or class name you choose to give to your project resources. – Sotirios Delimanolis Oct 11 '12 at 00:29