How to deploy REST based web services to Liberty Profile ?


In my last blog entry I described how to install Liberty Profile and to configure an Eclipse based development environment.  In this entry, I will show you how to develop & deploy a “Hello World” complexity REST based web service.

Official JAX-RS / Liberty profile is available on IBM Documentation web site.  When developing or debugging REST based services, it is always good to know that IBM’s WebSphere Liberty profile is using Apache’s Wink implementation behind the scene.

Unlike some other Java based application servers (this one and this one for example), WebSphere Liberty Profile does not perform many under covers magical for you, in particular it does not register an application context, you will need to write (one line of) code to do that.

That being said, the process is quite similar for every application server and IDE :

1. Create a web based project

 

Choose a Project Name, select the runtime for deployment and uncheck the “Create EAR” option

2. add a POJO class that will serve as “resource”

Select a package name and class name.

Type the following code :

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;

@javax.ws.rs.ApplicationPath("resources")
@Path("/test")
public class Demo extends javax.ws.rs.core.Application {

    @Context
    private UriInfo context;

    @GET
    @Produces("application/xml")
    public String getXml() {
        return "<xml>Hello Rest World !</xml>";
    }

    @PUT
    @Consumes("application/xml")
    public void putXml(String content) {
    }
}

3. add your business code for the PUT and GET methods

4. Before deploying – Add JAX-RS “feature” to your server configuration

This will tell the Liberty kernel to load the JAX-RS server side implementation. You do not need to restart your server when adding / removing features.

5. Deploy and Test


At this stage, Eclipse’s browser will open on the application default URL and will display an error message.  This is normal as we did not define a landing page or default servlet in this project (index.jsp or index.html) for example.

To access the REST web service, use this URL pattern :

http://<hostname>:<port number>/<project name>/<application path>/<path>

which translates for this example to

http://localhost:9080/TestREST/resources/test

 

Et voilà, you just created, deployed and tested your first REST based web service on WebSphere Liberty Profile.

Enjoy !

 

, , , , , ,

  1. #1 by MikeC711 on 20/01/2013 - 03:29

    Best part about this sample is its simplicity. Helped us avoid the normal pitfalls of putting the basic plumbing in place … without putting in lots of un-needed code. Big help. With something this simple, you can get the bugs out of your env.

  2. #2 by Mani Krishnan on 20/06/2013 - 04:27

    Nice post, Mike. It is helping me try the WorkLight RedBook sample easily.

  3. #3 by huahsin68 on 27/08/2013 - 11:56

    Great post. I have your sample up and running. BTW, your tutorial is very different from the rest where you didn’t mention any Jersey Servlet object in web.xml, is that mean since the the backbone of Liberty Profile is actually an Apache Wink, thus everything will follow Wink standard implementation? Please correct me if I’m wrong.

  4. #4 by Sébastien Stormacq on 27/08/2013 - 12:19

    You’re correct.

  5. #5 by andrew on 18/02/2014 - 09:46

    Thanks for posting this great intro to using REST in liberty.

  6. #6 by Vince Thyng on 31/07/2014 - 22:57

    Really nice and simple example. Thank you very much!

  7. #7 by Anand on 16/12/2014 - 16:36

    Very concise and precise. Thank you!

  8. #8 by Vardaan on 07/10/2015 - 21:48

    Awesome Example

(will not be published)