Search code examples
javaeclipserestjersey

Web service error HTTP Status 404 - Not Found


I am following a simple webservice tutorial and can't seem to interact with the Java code. I suspect my web.xml has an error but I'm not sure. There are no obvious errors and the index.jsp is server without any problems.

So, when I'm running it on the server, it opens index.jsp and I then try the following urls, but I'm getting 'HTTP 404 Errors'

Here is what i have
Dynamic web project with jersey libs imported. A note on this - I got an error for class not found and saw that I had to use Glassfish.org... instead of the com.sun one, don't know why, but there ya go. enter image description here

My web.xml is as follows. No errors.

<?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>RestApi</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>
  <display-name>Rest Web Services App by me</display-name>
  <servlet>
    <servlet-name>exampleServlet</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>com.rest.example</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>exampleServlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

My java class is as follows. No errors.

package com.rest.example;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorld {
    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg){
        String output = "Welcome to the world of Rest : "+msg;
        return Response.status(200).entity(output).build(); 
    }

}

Solution

  • You are using the old Jersey 1.x property

    com.sun.jersey.config.property.packages
    

    For Jersey 2.x it should be

    jersey.config.server.provider.packages
    

    As a general rule, anything where you see com.sun.jersey is for Jersey 1.x and org.glassfish.jersey is for 2.x.