You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Creating a Simple Application

This guide will create a simple JAX-RS application that can respond to requests at "/helloworld" with a "Hello world!" plain text resource. While not entirely RESTful, this guide is to show how to create a simple application and how to package it for consumption in a web container. The application will be packaged in a WAR file (which is like a JAR/ZIP file except with special files in certain locations and a defined layout). It can be deployed in any web container (i.e. Jetty, Tomcat, Geronimo, etc.).

Create the Root Resource

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

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.

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

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

package org.apache.wink.example.helloworld;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

public class HelloWorldApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(HelloWorldResource.class);
        return classes;
    }

}

Compile your classes

Using the Apache Wink distribution's JARs in your classpath, compile the 2 classes above.

Create a web.xml

Next, you need to 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. Also, any requests that begin with /rest/ will be handled by the Apache Wink JAX-RS servlet. So, the suffix of the request URL requires "/rest/helloworld" to reach the HelloWorld resource.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Hello world Web Application</display-name>
    <servlet>
        <servlet-name>HelloWorldApp</servlet-name>
        <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>org.apache.wink.example.helloworld.HelloWorldApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorldApp</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

Package your web application into a WAR

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

Not every JAR in the lib directory is necessary for all environments but you should read your documentation for more information on what is and is not required.

WEB-INF/classes/org/apache/wink/example/helloworld/HelloWorldApplication.class
WEB-INF/classes/org/apache/wink/example/helloworld/HelloWorldResource.class
WEB-INF/lib/activation-1.1.jar
WEB-INF/lib/commons-lang-2.3.jar
WEB-INF/lib/jaxb-api-2.1-sources.jar
WEB-INF/lib/jaxb-api-2.1.jar
WEB-INF/lib/jaxb-impl-2.1.4-sources.jar
WEB-INF/lib/jaxb-impl-2.1.4.jar
WEB-INF/lib/json-20080701.jar
WEB-INF/lib/jsr311-api-1.0.jar
WEB-INF/lib/slf4j-api-1.5.8.jar
WEB-INF/lib/slf4j-simple-1.5.8.jar
WEB-INF/lib/stax-api-1.0-2.jar
WEB-INF/lib/wink-common-0.1-incubating-SNAPSHOT.jar
WEB-INF/lib/wink-server-0.1-incubating-SNAPSHOT.jar
WEB-INF/lib/xercesImpl-2.6.2.jar
WEB-INF/web.xml

Install the WAR into your environment

Most web container environments will take a WAR file and deploy it without any further configuration required. However, you should note what Context Root that your web application is deployed to, or change it to something you want.

The context paths combine to be like:
http://<hostname>/<web application context root>/<servlet url mapping path>/helloworld

So if the environment deployed the WAR file to a context root of "/helloworldapp", then the following URL would be required to reach the HelloWorldResource.

http://<hostname>/helloworldapp/rest/helloworld

  • No labels