HTTP Status 404 - Servlet Jersey REST Service is not available

HTTP Status 404 - Servlet Jersey REST Service is not available

So I am trying to run a very simple example from http:-//www.ibm.-com/developerworks/web/library/wa-aj-tomcat/#download of building a REST Jersey Webservice
Here is what i did
-Named the project Jersey (Dynamic Web Project)
-Created a package "sample.hello.resources"
-Added a class called "HelloResource"
-The code of the class HelloResource is


package sample.hello.resources;  
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 HelloResource {  
    @GET 
    @Produces(MediaType.TEXT_PLAIN)  
    public String sayHello()  
    {  
        return "Hello Jersey";  
    }  
}             
package sample.hello.resources;
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 HelloResource {
 @GET
 @Produces(MediaType.TEXT_PLAIN)
 public String sayHello()
 {
  return "Hello Jersey";
 }
}           
 

-Changed the web.xml file to 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_2_5.xsd" id="WebApp_ID" version="2.5">  
  <display-name>Jersey</display-name>  
  <welcome-file-list>  
    <welcome-file>index.html</welcome-file>  
  </welcome-file-list>  
    
  <servlet>  
    <servlet-name>Jersey REST Service</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>sample.hello.resources</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
    
  <servlet-mapping>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <url-pattern>/rest/*</url-pattern>  
  </servlet-mapping>  
    
</web-app> 
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Jersey</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
 
  <servlet>
    <servlet-name>Jersey REST Service</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>sample.hello.resources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
 
</web-app>

-I then added the project to my server (TOMCAT in eclipse)
-I then typed the followoing url http://localhost:8080/Jersey/rest/hello
And i get the following message

HTTP Status 404 - Servlet Jersey REST Service is not available  
type Status report  
message Servlet Jersey REST Service is not available  
description The requested resource (Servlet Jersey REST Service is not available) is not available. 
I finally managed o solve the problem. Just needed to dump the rar libraries in the correct folder.

Check if jersey-server library is there in the Path.
Sometimes, if your version doesn't match, then there's problem in jersey server startup.
I too faced the problem. Later with jersey-server-1.9.1.rar, the problem got solved. 
 
There's your problem: build path != runtime path. The libraries need to be in the WEB-INF/lib directory of your web app.

This is a typical case where IDEs behave differently than standalone servers. I never run Tomcat in an IDE, it just complicates matters IMO.

I exported the web app as a .war file and started Tomcat independently (external to Eclipse IDE).
Now, the example works OK.

There is definitely a problem running Tomcat through Eclipse IDE.

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright