0

I just installed netbeans on my mac (os x lion) and i'm trying to create a restful web service with jersey.

When I go to my localhost:8080/HelloWorld/test/printHello I get a 404 Exception...so I thought i'd look into my web.xml..but I can't find the file.

Any pointers where I should look and what I should do? My java class is posted below.

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

    @GET
    @Path("printHello")
    @Produces("text/plain")
    public String printHello() {
        return "Hello there!";
    }
}
user1467188
  • 617
  • 1
  • 10
  • 28

2 Answers2

4

As per servlet 3.0 spec, web.xml is optional.

However while creating a web project in Netbeans/Eclipse we will be given an option to have a deployment descriptor. If no option, we can create web.xml explicitly. And yes, we need a web.xml to do restful web services using Jersey as its API exposes ServletContainer which takes care of class mapping based on URL.

Hope this helps!

kausal_malladi
  • 1,542
  • 3
  • 14
  • 30
1

In Netbeans the web.xml is located in

WAR-Project with Maven
    - Web Pages
        - WEB-INF
            - web.xml

In the folder structure its located in:

WAR-Project with Maven
    - src
        - main
            - webapp
                - WEB-INF
                    - web.xml

There you can define the Jersey Servlet:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- Jersey -->
    <servlet>
        <servlet-name>Jersey Web Application</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>package.path.to.your.rest.service;</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

Now you can access your REST-Service under following URL:

localhost:8080/HelloWorld/rest/test/printHello
Yser
  • 2,086
  • 22
  • 28