In this article I will show how to build basic RESTful Web Services in JAVA for Web Application using JAX-RS: The Java API for RESTful Web Services and its open source implementation: Jersey together with XStream. I will also show how easy is to extend Jersey to work with your favorite XStream library and that testing Web Services can be a fun with the outstanding soapUI application.
Jersey, RestEasy, Restlet …
The are many libraries in JAVA which provide functionality for building RESTful services. Jersey and RestEasy are both JAX-RS implementations. Restlet is a very complex framework supporting lots of features available in many editions (including the one for Google App Engine). There are many factors that help decide which library to choose but the are not covered in this article. My choice is Jersey. But I don’t claim it is the best. I just like it.
Jersey provides Test Framework that allows you to test RESTful Web Services on a wide range of extensible containers, including Grizzly and Glasshfish. You can write tests using JUnit 4 by extending JerseyTest class in your test cases. If you want to find out more read this blog post describing usage scenario.
For functional, integration and load testing I suggest using soapUI. soapUI is a great, open source tool for testing Web Services, including RESTful services. It lets you create test suites within one click only. You can install it as an Eclipse plugin, but I prefer (and use) the standalone version.
XStream – XStream is simple and fast object serialization library. Jersey natively supports Java for XML binding API (JAXB), but I will show how to extend it to support XStream – in case if you have your object model defined using XStream. You can download the latest version here: XStream download
Creating a project
Create a Dynamic Web Project in Eclipse, call it whatever you want and import all required libraries from Jersey and XStream:
jettison-1.1.jar – used by XStream for JSON serialization
xpp3_min-1.1.4c.jar – used by XStream when unmarshaling
Eclipse - package explorer
Object model classes
Let’s create object model using XStream annotations. In order to do it you can use this useful tutorial.
Person object container with some data manipulations methods:
// methods omitted
// methods omitted
Creating XStream provider
Extending Jersey to support XStream is easy. You need to create a class annotated with @Provider that marks it as a extension interface. The class must implement MessageBodyReader and MessageBodyWriter to provide Java object serialization and deserialization. In Jersey you can extend AbstractMessageReaderWriterProvider to write your own provider. The provider supports Java classes annotated with @XStreamAlias.
Please note the class is also annotated with @Produces (required by MessageBodyWriter) and @Consumes (required by MessageBodyReader) which means that it can read and write object to and from XML and JSON format.
Creating RESTful resource – PersonResource
// class body omitted
The resource class is annotated with @Path that identifies the URI path that a resource class or class method will serve requests for. The other annotation is @Singleton which is not a part of JAX-RS API and marks the resource as a singleton (one instance per web application). You can mark any resource as singleton – including XStreamProvider, if needed.
The above methods depend on the path used (person/xml or person/json) returns serialized People object. Creating @PUT – create new Person, @POST – modify existing record, @DELETE – delete record in JAX-RS is analogous:
That’s it. We have a resource that operates on Person Java object allowing: retrieval, creation, edition and removal. Easy. Tests omitted.
Deploying and running
To deploy the services to the server some small configuration needs to be done. Edit your web.xml file and add com.sun.jersey.spi.container.servlet.ServletContainer with init param com.sun.jersey.config.property.packages with packages that contain the resources. Remember, the XStreamProvider class is also a resource!
Jersey exposes WADL file out-of-the-box under the following URL: /api/application.wadl (/api/* is a Jersey servlet mapping path, see servlet mapping in web.xml). I use this URL to create a soapUI project (the web server must be running of course). Run the soapUI and create new project. In the initial WSDL/WADL field type the URL of application WADL mentioned before:
soapUI new project definition with initial WADL
Click OK. The project is created, all methods are ready to be tested: soapUI creates default requests for all methods found in the WADL.
soapUI project details
By using soapUI you can browse content of the WADL file, you can test you requests, you can create test suites, load cases and many more.
Executing requests in soapUI
Find default requests for getAllAsXml and getAllAsJson methods, open and execute them. You should see the output of a request as in the following image:
Similarly, you can verify other methods. Isn’t it just simple?
soapUI – next steps
Having defined default requests in your project it is time to create a test suite (soapUI generates it automatically, if you wish). Create test cases, test steps, load tests and make sure your application is working perfectly. soapUI is a very popular tool, it is also very easy to learn. I suggest reading the documentation on soapUI web site to get familiar with its wide range of features.
I used to work with soapUI for testing SOAP Web Services, and I really enjoyed it. I think, the tool can be easily used for testing RESTful web services too, and now creating Web Services (not only RESTful) is a piece of cake. Maybe it is the right time to focus more on testing them? What do you think?
Download the application
The web application used in this article can be downloaded here: jersey-with-xstream. It contains Java source files.
Share your opinions and experience with us by leaving a comment or meet us on Twitter: @GOYELLO.