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 from
  2. Extract
  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
    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;
    public class HelloWorldResource {
     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.


  1. Have you a example with glasfish beacause in can't use queryParam

  2. Does it work with any version of Websphere 6.1 ? or is there any fixpack needed ?

  3. It seems that it doesnt't work for WAS, with RAD 7.5.3 IDE in my case.

    I get this:
    Could not be defined due to: (com/sun/jersey/spi/container/servlet/ServletContainer) bad major version at offset=6
    This is often caused by having a class defined at multiple
    locations within the classloader hierarchy. Other potential causes
    include compiling against an older or newer version of the class
    that has an incompatible method signature.
    Dumping the current context classloader hierarchy:
    ==> indicates defining classloader
    Local ClassPath: C:\workspaceREST\pruebaREST\WebContent\WEB-INF\classes;C:\workspaceREST\pruebaREST\WebContent\WEB-INF\lib\asm-3.1.jar;C:\workspaceREST\pruebaREST\WebContent\WEB-INF\lib\jersey-bundle-1.4.jar;C:\workspaceREST\pruebaREST\WebContent\WEB-INF\lib\jsr311-api-1.1.jar;C:\workspaceREST\pruebaREST\WebContent
    Delegation Mode: PARENT_FIRST
    Local ClassPath:
    Delegation Mode: PARENT_FIRST
    [4] org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@41864186
    [5] sun.misc.Launcher$AppClassLoader@21c021c0
    [6] sun.misc.Launcher$ExtClassLoader@3fee3fee
    ---Original exception---
    java.lang.UnsupportedClassVersionError: (com/sun/jersey/spi/container/servlet/ServletContainer) bad major version at offset=6

  4. You probably took the Jersey that is compiled with Java 6. Try to find one compiled with Java 5. Also check that you don't compile using Java 6.

  5. I don't get a responce. Don't you need to configure the package of the resource in the XML Servlet section as init-params?

    Something like

  6. I didn't have no init-params. It has work with web.xml as described.

  7. Exception in thread "main" java.lang.NoSuchMethodError: com/sun/jersey/core/spi/component/ProviderServices.(Lcom/sun/jersey/core/spi/factory/InjectableProviderFactory;Lcom/sun/jersey/core/spi/component/ProviderFactory;Ljava/util/Set;Ljava/util/Set;)

    It's about 3hours that I fight with this Exception! I build a webService on Websphere 7, and another client test but always the same exception!

  8. thanks so much, it works fine

  9. Thanks alot, It Works fine. If anyone has error, check jar files carefully

  10. Thanks alot. Followed the instructions and worked like a charm! ;)

  11. Exception in thread "main" java.lang.NoClassDefFoundError: java.util.logging.LogManager (initialization failure)
    at java.lang.J9VMInternals.initialize(
    at java.util.logging.Logger.getLoggerWithRes(
    at java.util.logging.Logger.getLogger(
    at com.sun.jersey.api.client.Client.(
    at java.lang.J9VMInternals.initializeImpl(Native Method)
    at java.lang.J9VMInternals.initialize(
    at com.mits.restclient.HelloWorldRestClient.main(