11

This is my first time using Eclipse, and is causing me to rage a lot.

I installed Tomcat 6.0, downloaded the Jersey libraries, and I followed the tutorials from : http://www.vogella.com/articles/REST/article.html#first_client

I created the Project Name as RestExample, and within that I have a package de.jay.jersey.first and within that I have a class HelloWorldResource, and here is what it looks like:

package de.jay.jersey.first;

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 HelloWorldResource {
// 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> ";
}
}

and my web.xml looks like

<?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>RestExample</display-name>
  <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>de.jay.jersey.first</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>

ANd I am trying to use curl as:

curl http://localhost:8081/RestExample/rest/hello

Apache Tomcat/6.0.35 - Error report

HTTP Status 404 - /RestExample/rest/Hello

type Status re port

message /RestExample/rest/hello

de scription The requested resource (/RestExample/rest/hello) is not available.

Apache Tomcat/6.0.35

The question is what should I change in the web.xml so that I can access that resource?

I tried RestExample/de.jay.jersey.first/rest/hello, and it still did not work. TOmcat is running without errors.

roymustang86
  • 8,054
  • 22
  • 70
  • 101
  • How did you deploy this application to Tomcat? Specifically, where is your web.xml located and where is your class file located? – Mark Thomas May 08 '12 at 15:37
  • I tried Tomcat 7.0 and it works fine – Timmo May 08 '12 at 15:40
  • I changed only minimal things like Servlet version from 2.5 to 3.0 in web.xml – Timmo May 08 '12 at 15:46
  • @MarkThomas: The web.xml is located in RestExample\WebContent\WEB-INF\ and the class file is located in RestExample\build\classes\de\jay\jersey\first\ – roymustang86 May 08 '12 at 16:19
  • 2
    classes need to be located under WEB-INF/classes so in this case WEB-INF/classes/de/jay/jersey/first/HelloWorldResource. Note if you are deploying through Eclipse it may be doing this for you in the background. Transparency of what is going on is one reason I prefer to get Eclipse to build the WAR and deploy it myself. – Mark Thomas May 08 '12 at 17:48

7 Answers7

13

It took me a lot of time to figure out why it wasn't working for me inspite of looking all over the web for solution. The mistake I was making was that package names were not up to date to the new jersey api. Here's what updated package names should look like (Web.xml):

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>RestExample</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>de.jay.jersey.first</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>

Notice that <servlet-class> and <param-name> are different(updated) from vogella tutorial. It may not be the answer to this particular question but might help someone. I found it from here.

gndps
  • 461
  • 3
  • 13
  • thanks @gndps you saved my day.Your answer deserves to be on top actually. I changed the project and moved the web.xml part as well but I forgot to change my package name. Your answer helped me. Thanks again – samir Feb 25 '16 at 06:27
  • @samir - what gndps means is not the name of your package, but the init param name where you provide your package name in web.xml. still the fix you have done is valid for your problem, but slightly different from the problem explained here. The init-param name for mentioning the packages differs with different jersey versions. – Mohamed Iqzas Nov 09 '17 at 06:57
2

Please add all the given Jars in your project

Project (Right Click)>Properties>Java Build Path>Libraries>Add JARs/Add External JARs

  1. asm-3.1.jar
  2. jersey-bundle-1.14.jar
  3. jersey-client.jar
  4. jersey-core.1.17.1.jar
  5. jersey-server-1.17.jar
  6. jersey-servlet-1.17.jar
saurabh
  • 21
  • 1
1

I tried it with Tomcat 7.0 and it works fine:

package de.jay.jersey.first;

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 HelloWorldResource {
// 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> ";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>RestExample</display-name>
    <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>de.jay.jersey.first</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>

Browsed to http://localhost:8084/RestExample/rest/hello and it works ok

Timmo
  • 3,142
  • 4
  • 26
  • 43
  • 1
    This config is good if you are on Jersey 1.x. Jersey 2.x has different container package name and property names. So, the same config wont work if you are using 2.x jars. Just thought I will mention it, so it will spare unnecessary trouble for someone who has 2.x jars in its classpath. – adityalad Aug 22 '17 at 07:02
1

i looked for a solution to the same problem for hours too.

this solved my problem:

if you use a Maven-Project (for example with archetype maven-archetype-webapp) and the class HelloWorldResource is implemented in the folder src/main/resources this class doesn't get compiled (for example then running "mvn clean package" or "run on server" in eclipse)

Implement HelloWorldResource in folder src/main/java instead and no more 404 occures.. (if you create Maven-Project with maven-archetype-webapp the folder needs to be manually created)

Celle
  • 11
  • 1
1

Check if your path have this bar '/' example: @Path('/path') in some cases this problem is only the missing bar!

0

If you are using Jersey 2.XX user ServletAdaptor instead. Like this,

    <servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
    <!-- <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> -->
eunelyky
  • 69
  • 7
-2

like the aticle says:

// Get the Todos
    System.out.println(service.path("rest").path("todos").accept(
            MediaType.TEXT_XML).get(String.class));
    // Get XML for application
    System.out.println(service.path("rest").path("todos").accept(
            MediaType.APPLICATION_JSON).get(String.class));
    // Get JSON for application
    System.out.println(service.path("rest").path("todos").accept(
            MediaType.APPLICATION_XML).get(String.class));

you try to specify the method path which you want to call