user1552294
user1552294

Reputation: 125

Simple REST Jersey example in Java not working

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>

Upvotes: 0

Views: 5360

Answers (1)

user1552294
user1552294

Reputation: 125

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...

Upvotes: 4

Related Questions