27

I've read through a number of posts, but just can't seem to solve my problem. You'll also see tons of posts very similar to this one, even the same tutorial. Even following them, I can't seem to get to the answer.

Essentially, I'm trying to follow the simple tutorial at: http://www.vogella.com/articles/REST/

I've made a few changes to make it compatible with Jersey 2.x

I'm using: Eclipse Tomcat 6 (Deployed/Run as within Eclipse) jaxrs-ri-2.0 I've enabled the JAX-RS Facet in Eclipse

Everything builds fine Tomcat starts fine within Eclipse I can get to a static page content via:

http://localhost:8080/RestTEST2/index.html

However, when I try to access my service via:

http://localhost:8080/RestTEST2/jaxrs/hello

I receive a 404 with "message not found" and "The requested resource (Not Found) is not available."

Here is my web.xml which is located at /WebContent/WEB-INF/web.xml

<?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" 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>TestREST2</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
  <description>JAX-RS Tools Generated - Do not modify</description>
  <servlet-name>JAX-RS Servlet</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>TestREST</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>  
  </servlet>
    <servlet-mapping>
      <servlet-name>JAX-RS Servlet</servlet-name>
      <url-pattern>/jaxrs/*</url-pattern>
    </servlet-mapping>
</web-app>

Here is my Java class:

package TestREST;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

// Plain old Java Object it does not extend as class or implements 
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@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> ";
  }   
} 

I also have a JAX-RS User Library configured and referenced that includes all the JAX-RS jars.

Thoughts on what would cause the web service to not be found?

Jason Allen
  • 370
  • 1
  • 3
  • 7

5 Answers5

28

Jersey 2.0 does not recognize init-param with name com.sun.jersey.config.property.packages (web.xml). Try to change it to jersey.config.server.provider.packages as described in ServerProperties.PROVIDER_PACKAGES.

UPDATE (2020): try this link for current apidocs ServerProperties.PROVIDER_PACKAGES:

Jim Ford
  • 1,095
  • 1
  • 13
  • 21
Michal Gajdos
  • 10,339
  • 2
  • 43
  • 42
  • 3
    Awesome. I've been fighting this for hours. This was soluton to my problem. I also had my package name mistyped in the xml as testREST, when it should have been TestREST, but this was the root of the issue. Thanks a ton. – Jason Allen Jul 17 '13 at 21:33
  • From your comment I've learned to change provider pkg to my own pkg. Thanks – Stalin Gino Nov 28 '14 at 20:03
  • I am trying to implement same project..... but same error persists.. I've tried all solutions provided here plus tried changing the tomcat server's location also.... – spt025 Jul 15 '15 at 09:18
  • Server is online but the problem is even after doing same things , I'm getting 404 error...!!! – spt025 Jul 15 '15 at 09:19
  • I found the answer for my problem. It was similar problem but with little twist. Have added as answer. – spt025 Jul 15 '15 at 09:33
6

Thanks a lot.. i've been fighting for it as well.

This combination worked for me: Tomcat 7.0.55 Eclipse Luna Java 1.6 Jersey 1.7

web.xml:

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.trgr.cobalt.cmdb.jersey.resource;com.trgr.cobalt.cmdb.jersey.beans;com.trgr.cobalt.cmdb.elasticity</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>
Suren Konathala
  • 3,497
  • 5
  • 43
  • 73
3

I would like to add one answer in this post.I was struggling with same problem for two days and couldn't find the solution. I tried all the possible solutions provided here but later on I realized that server was giving error of

 org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException

This link answers gives the solution if somebody is stuck like me.. org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException

Actually first answer given in this post suggest the solution which is for jersey 2.x bundle and if you are using jersey 1.x then it will keep on giving error.

Kindly refer to link given in answer for further reference

Community
  • 1
  • 1
spt025
  • 2,134
  • 2
  • 20
  • 26
0

Simply by modifying the configuration of Apache Tomcat v7.0 while creating Dynamic Web Project, to include REST, I solved my problem. It is not enabled by default.

0

Recently I found myself stuck in this problem. I have got it resolved by following these two points as the solution:

  1. By making the src/main/java as the source directory, and keeping the Project Structure as shown below:

enter image description here

  1. Changing com.sun.jersey.config.property.packages to jersey.config.server.provider.packages in the web.xml