Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The following example project will create produce a simple JAX-RS application that can respond to requests at "/helloworld" with a "Hello world!" plain text resource. While not entirely RESTful, this example project is to show how to create a simple application and how to package it for consumption in a web container.

The application is packaged in a WAR file (which is similar to a JAR/ZIP file, except with special files in certain locations and a defined layout). It can be deployed in any web container, for example: Jetty, Tomcat, Geronimo, etc..

Perform the following steps in order to create the "helloworld" example application.

Step 1

Creating the Root Resource

First, create a resource that will be used for HTTP GET requests to "/helloworld".

Code Block
package org.apache.wink.example.helloworld;

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

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

    @GET
    public String getMessage() {
        return "Hello World!";
    }
}

As shown above, the Java class is just a plain old Java object that has JAX-RS annotations.

Step 2

...

Creating a javax.ws.rs.core.Application sub-class

For non-JAX-RS aware web container environments (most environments are currently non JAX-RS aware), a javax.ws.rs.core.Application sub-class needs to be created which returns sets of JAX-RS root resources and providers. In the following example, there is only one root resource that will need to be returned.

...

Using the Apache Wink distribution's JARs in the classpath, compile the two classes from the previous example.

Step 3

Creating a web.xml file

Next, create a web.xml deployment descriptor. The deployment descriptor details information about the web application in the WAR. In this case, it says that the Apache Wink JAX-RS servlet should be initialized with a HelloWorldApplication instance.

...

Layout the application as follows and create a WAR file from the base directory (the one before WEB-INF). Create a WAR by running "*jar cvf helloworld-jaxrs.war * *" from the base directory.

...