0

I wrote a very simple example using Jersey. I downloaded the latest jar files from the jersey website into lib folder in WEB-INF. My class and web.xml are below.

When I provide URL localhost:8080/SimpleJersey/rest/test I get 404 error (not found).

However when I use Maven, it works. I use Eclipse Kepler, Glassfish 4 server and Java 7.

What am I doing wrong in the non-Maven version?

Thanks.

Class:

package com.simplejersey;

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

@Path("/test")
public class MyResources
{

    @GET
    @Produces("text/plain")
    public String getIt()
    {
        return "Hello there!";
    }
}

Web.xml:

<?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>SimpleJersey</display-name>

  <servlet>
        <servlet-name>jersey-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>com.simplejersey</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping> 

</web-app>
Balder
  • 8,623
  • 4
  • 39
  • 61
user1552294
  • 125
  • 1
  • 10
  • I should add that I get no error messages in Eclipse. just 404 error in the browser. Perhaps it's something to do with dependencies since it works when I use Maven. – user1552294 Dec 04 '13 at 00:00
  • I solved it reading this post: http://stackoverflow.com/questions/17709039/jersey-rest-web-service-tomcat-eclipse-and-404s?rq=1 The issue is (quoting from the abovementioned post): "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..." – user1552294 Dec 04 '13 at 04:45
  • Read this post http://stackoverflow.com/questions/20784531/jersey-services-with-tomcat-and-eclipse, maybe it can be useful for you – jjmartinez Mar 24 '14 at 10:00

1 Answers1

4

I found the solution in this post by Michal Gajdos: Jersey REST Web Service, Tomcat, Eclipse and 404's

The issue is (quoting from the abovementioned post):

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 (link)."

Be carefeul when you copy web.xml from websites that show older solutions or versions (like I did). Jersey is being updated too...

Community
  • 1
  • 1
user1552294
  • 125
  • 1
  • 10