Showing posts with label Jersey. Show all posts
Showing posts with label Jersey. Show all posts

Friday, April 3, 2009

Hello World! REST service using Jersey and WebSphere Application Server v6.1 (WAS)

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:

  1. Download jersey-archive-1.0.2.zip from http://jersey.dev.java.net/.
  2. Extract jersey-archive-1.0.2.zip.
  3. Go to your favorite tool for WAS development. Mine was Rational Software Architect 7.5 (RSA) but the same steps works for 7.0.
  4. 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.
  5. 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.
  6. 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.
  7. 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:
    1. @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
    2. @GET – annotated method (sayHello) is available through HTTP GET method
    3. @Produces("text/plain") – method will return plain text
    4. @QueryParam("world") – with this annotation we are mapping URI parameter world to method parameter world
  8. 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:
  1. Created a new Java project called HelloWorldRestJava.
  2. Created new folder called lib.
  3. Copied jars from WEB-INF/lib to lib folder. Added them to project classpath.
  4. 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);
     }
    }
    
  5. 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.