I was wondering how easy it is to create Hello World REST service using Jersey and deploying it to WAS 6.1. Long story short: It is really easy. So here are the steps:
- Download jersey-archive-1.0.2.zip from http://jersey.dev.java.net/.
- Extract jersey-archive-1.0.2.zip.
- Go to your favorite tool for WAS development. Mine was Rational Software Architect 7.5 (RSA) but the same steps works for 7.0.
- Create new Dynamic web project (HelloWorldRest). In RSA 7.5 you should choose WAS 6.1 as your target platform. Also choose Servlet spec 2.4.
- Copy jersey jars from lib folder. That’s the folder where you extracted jersey archive in step 2. Put them in the WEB-INF/lib folder.
- Now you must add Servlet configuration and mapping. You can do this through wizard or you can copy the following in web.xml just after display-name element:
Jersey Servlet
ServletContainer
ServletContainer
com.sun.jersey.spi.container.servlet.ServletContainer
ServletContainer
/resources/*
With this in place we have configured Jersey servlet. It will serve REST services with pattern resources in URI.
- Now we can create service implementation class. We are creating simple POJO class (in JAX-RS term Root Resource Class) with one method like one below:
/**
*
*/
package org.example;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
@Path("/helloworld")
public class HelloWorldResource {
@GET
@Produces("text/plain")
public String sayHello(@QueryParam("world") String world) {
return "Hello " + world;
}
}
The interesting bits are four annotations:
- @Path("/helloworld") – this annotation exposes methods in this class through helloword path. Annotation value is the thing that we will add after resources in URI to call this service
- @GET – annotated method (sayHello) is available through HTTP GET method
- @Produces("text/plain") – method will return plain text
- @QueryParam("world") – with this annotation we are mapping URI parameter world to method parameter world
- Deploy project on server!
And that’s it. Now we can test service using browser. If you enter following URI: http://localhost:9080/HelloWorldRest/resources/helloworld?world=World! you should get response in clear text, as expected: Hello World!.
Next we can create a simple stand alone client for this service. I have:
- Created a new Java project called HelloWorldRestJava.
- Created new folder called lib.
- Copied jars from WEB-INF/lib to lib folder. Added them to project classpath.
- Created new class with main method:
package org.example;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
public class HelloWorldRestClient {
public static void main(String[] args) {
Client client = new Client();
WebResource webResource = client.resource("http://localhost:9080/HelloWorldRest");
String response = webResource.path("resources").path("helloworld")
.queryParam("world", "World!").get(String.class);
System.out.println("Response: " + response);
}
}
- Run the client as Java application.
If you do the same you should see Response: Hello World! in the console. Now when you saw how easy it is, you can continue to play with Jersey.