REST APIs have become a popular and important way to interact with computing services in an API economy with the growing popularity of microservices architectures. In this blog I will discuss how simple it is to create RESTful services with WAS Liberty compared to open source alternatives. When I started this study, I was new to the RESTful services.
RESTful services have been in the industry for quite a while. JAX-RS is Java API for RESTful web services. It is a collection of interfaces and annotations that help simplify REST server-side code. It converts a simple POJO (Plain Old Java Object) into a RESTful service and exposes the resources for consumption.
RESTful style of service is popular because it is straight forward for the end user to consume. A simple HTTP call will provide the data either in JSON or XML format making it convenient to develop and debug cross platform web and mobile applications. There are three components to a REST API:
- The resource URL – uniquely identifies the resource and that all necessary parameters are also passed to it via the URL
- The action (GET, POST, DELETE)
- The format the data is returned (JSON or XML)
The main advantage of RESTful style of service is that it uses HTTP protocol for content delivery and can be scaled to service different clients across many platforms and programming languages.
WAS Liberty and RESTful services
Traditional WebSphere App Server started supporting JAX-RS many years ago in V8.0 as a Feature Pack and has it built-in since V8.5.
WebSphere Liberty is a fully Java EE 7 certified server and thus fully supports RESTful services. Liberty also provides support for a variety of databases and for this study I took MongoDB (stores customer profile info) and converted a POJO class into RESTful service for retrieving customer profile info. I also took advantage of the UI provided by the Eclipse WAS liberty plug-in (WebSphere Developer Tools) to configure MongoDB, thus avoiding editing of XML files.
Installation of Liberty is very simple and quick. Just download WAS Liberty zip file from https://developer.ibm.com/wasdev/downloads/ .
You can find instructions to download and install Eclipse and WebSphere Developer Tools below https://developer.ibm.com/wasdev/downloads/liberty-profile-using-eclipse/ .
Once you have Eclipse and Liberty it installed, we can enable support for JAX-RS and MongoDB.
- In Eclipse, double click and open the Liberty profile configuration from the “Server” view.
- Select feature manager in the design view and add the feature JAX-RS and MongoDB.
- Save the configuration file.
- Additional configuration is required for MongoDB to set the server, port and database name, and the necessary library files. These settings can be added via the UI or directly in the server.xml. As a newbie I preferred the feature provided by WAS Liberty to do it via UI in Eclipse.
We are now ready tocreate the customer profile RESTful service. Here are the steps:
- In Eclipse with Liberty Plugin (the one you installed earlier), create a dynamic web project.
- Create a class that extends javax.ws.rs.core.Application.
- In the constructor, initialize the connection to the MongoDB.
- Specify the @ApplicationPath annotation and set the root directory for the RESTful service.
- Add a new POJO class with various methods and each one will return the necessary data accessing the backend database.
Our new service can be invoked from a browser: http://localhost:9080/cfb/rest/getUserProfile.
Note: The backend database access is not discussed here since it is beyond the scope of this blog post. Refer to MongoDB online resources for further details.
I implemented the same functionality in Tomcat 8.0 and WildFly 9.0.1 (Open Source version of JBoss EAP). Tomcat out of the box does not support JAX-RS. Therefore, I had to look for a third party tool and decided to use Jersey . WildFly had its own implementation called RESTEasy .
There were minor implementation changes with respect to converting the POJO to RESTful services between the three providers. The main difference lies in configuring where WAS Liberty provides a developer with an Eclipse based GUI to do it while the other two had to be done manually by editing XML files which are prone to errors. Also, for configuration, WAS Liberty uses only one file (called server.xml) whereas Tomcat uses two files, and JBoss needs three files.
After overcoming the initial learning curve and some additional configuration steps with Tomcat and WildFly, I feel that other than the differences in a number of configuration files, overall the speed of development of RESTful services is roughly similar with all three runtimes. Furthermore, the Java code for my REST service is fully portable between all three runtimes and all that is required to make it work are configuration changes specific for each server runtime.
(e.g. Jersey or other 3rd party)
|GUI for developer configuration||
(Eclipse based UI)
(edit XML files by hand)
(edit XML files by hand)
|# of configuration files to deal with||1||2||3|
In this article I compared a small subset of an overall developer experience. You can read about more detailed evaluation in this blog post: “ Lightweight Java servers and developer view on the App Server (update) “.