Search code examples
javaeclipserestjersey-2.0

Java dynamic webserver - unable to read 'hello world' jersey resource


Im using jersey 2.0 and i have installed jersey jar files into eclipse using this site

the issue i am having is i cannot load a hello world project.

here is my project structure:

enter image description here

and here is the web.xml located in WEB-INF:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>com.vogella.web.filecounter2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <!-- Register resources and providers under com.vogella.jersey.first package. -->
    <init-param>
        <param-name>com.vogella.web.resources</param-name>
        <param-value>com.vogella.web.filecounter2</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>

and here is the hello world class:

 package com.vogella.web.resources;

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

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

}

The issue i am having is when i run the server it says resource not found. Here is the link:

http://localhost:8080/com.vogella.web.filecounter2/rest/hello

UPDATE: Here is a list of all jar files i have:

enter image description here

and here is the error i get when it try to view the resource either in eclipse or on chrome:

enter image description here


Solution

  • The code in your web.xml needs a correction.

    <init-param> 
        <param-name>com.vogella.web.resources</param-name> 
        <param-value>com.vogella.web.filecounter2</param-value> 
    </init-param>
    

    The param-name and param-value fields are not correct. Change the param-name to jersey.config.server.provider.packages and param-value to com.vogella.web.resources.

    The correction should look like

    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.vogella.web.resources</param-value>
    </init-param>