Apache Wink : JAX-RS Getting Started
This page last changed on Oct 13, 2009 by bluk.
The following example project will 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 and Geronimo. Perform the following steps in order to create the "helloworld" example application.
First, create a resource that will be used for HTTP GET requests to "/helloworld".
As shown above, the Java class is just a plain old Java object that has JAX-RS annotations.
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.
Now 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.
In addition, any requests that begin with /rest/ will be handled by the Apache Wink JAX-RS servlet. So, the request URL would be "/rest/helloworld" to reach the HelloWorld resource.
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.
Not every JAR in the lib directory is necessary for all environments. Read the documentation for more information about the requirements.
Most web container environments will take a WAR file and deploy it without any further configuration required. However, note the "Context Root" of the web application, or change it as required.
The context paths combine as follows:
If the environment deployed the WAR file to a context root of "/helloworldapp", then the following URL would be required to reach the HelloWorldResource.
|Document generated by Confluence on Nov 11, 2009 06:57|