REST Jersey Tomcat 7

22,597

I've completed this tutorial, and successfully launched your example.

My tip: tutorial you are trying to complete is 4-years old, so is not so actual. Better download full working example with jersey 2.0.

Your possible errors:

  1. Firstly you need jersey-archive-1.16 version. Jersey's new version
    no longer contains class com.sun.jersey.spi.container.servlet.ServletContainer which you are refering to inside web.xml
  2. Secondly: make sure your application's context-root is really set to de.vogella.jersey.first. If you're using eclipse check file: org.eclipse.wst.common and its context-root parameter.
  3. And of course make sure your project compiles correctly and on the classpath are at least four jars: asm.jar, jersey-core.jar, jersey-server.jar, jersey-servlet.jar.
Share:
22,597
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    Getting 404 error, when accessing the

    Hello.java

    package de.vogella.jersey.first;
    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> ";
      }
    
    } 
    '
    

    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" 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>de.vogella.jersey.first</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.vogella.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> 
    

    This has been taken from the http://www.vogella.com/tutorials/REST/article.html, but when I run the http://localhost:8080/de.vogella.jersey.first/rest/hello

    . I get 404 error .

    Can someone please help me on this.

  • learner
    learner over 10 years
    Above coding context root is correct find Coding package name and web.xml
  • G. Demecki
    G. Demecki over 10 years
    @Boopathi, doesn't have to be correct. During project creation inside eclipse, context-root is automatically being set to the project name. So this can also be an issue.