RESTEasy Tutorial with Eclipse and Tomcat With Examples

Welcome to RESTEasy Tutorial. RESTEasy is the JAX-RS implementation provided by JBoss project. We can use RESTEasy to create restful web services. RESTEasy provides tighter integration with the JBoss Application Server but we can deploy it on any servlet container. So today we will learn how to create restful web service using RestEasy framework and deploy in Tomcat servlet container to test it.

RESTEasy Tutorial

Some of the features of RESTEasy framework are:

  • JAX-RS API compliant – so mostly you need to plug it with JAX-RS API coding to create rest web services.
  • Runs on almost any servlet container supporting Java 6 or higher
  • Provides support for writing client programs using JAX-RS 2.0 client API. We will look into test program too.
  • Rich set of providers – XML, JSON, YAML, Multipart, XOP, Atom, etc.
  • OAuth2 and Distributed SSO with JBoss AS7
  • EJB, Seam, Guice, Spring, and Spring MVC integration

RESTEasy Example

Below are the URIs we will be exposing in our RestEasy web service implementation.

URI HTTP Method Description
/employee/add POST Add an employee
/employee/getDummy GET returns a dummy employee object
/employee/{id}/get GET Get the employee with ‘id’ in the URI
/employee/getAll GET Get all employees
/employee/{id}/delete DELETE Delete employee with ‘id’ in the URI

RESTEasy Example Eclipse Project

Below image shows our final project structure. First of all create a “Dynamic Web Application” in Eclipse and then convert it to Maven project to get the web project skeleton.

RESTEasy-Example-Project-371x450

Let’s look into different components of our RestEasy web services project.

RESTEasy Example Model Classes

We have two model classes – Employee for employee object and GenericResponse for sending client response object for status, message and error code.

Notice that both the java beans are annotated with @XmlRootElement annotation. This is required for JAXB API for converting objects to xml and vice versa.

RESTEasy Example Service Classes

Let’s first create an interface for all the operations we are trying to expose in our restful web service.

Below is the implementation class for above employee service interface.

We are using only JAX-RS API annotations such as @Path, @PathParam, Response etc. Notice that I am using javax.ws.rs.core.Response as response object in some of the methods where I want to send the HTTP status code other than 200.

RESTEasy Configuration

We haven’t done anything related to RestEasy till now, it’s time now to configure RestEasy as our JAX-RS API implementation for our restful web service.

First step is to extend javax.ws.rs.core.Application class and override few of the methods to inject our service class implementation.

Next step is to add RESTEasy maven dependencies in our pom.xml file as shown below.

Final step is to configure RESTEasy servlet class in deployment descriptor as front controller.

That’s it. Our web service is ready. You can see how easy it was to plugin RestEasy as our JAX-RS implementation for restful web service.

RESTEasy Restful Web Service Test

I am using Chrome Postman extension to perform testing for our web service. Below are some of the test cases with response.
One common part is the headers in all the requests as shown in below image.

HTTP-Request-Headers-450x123

  1. getDummy
  2. HTTP-Request-Headers-
  3. get
  4. getAll
  5. RESTEasy-web-service-delete-error-
  6. delete
  7. add error – HTTP response 422
  8. RESTEasy-web-service-delete-error-
  9. delete error – HTTP response 404
  10. RESTEasy-web-service-delete-error-

As you can see that all the tests passed with flying colors, our rest web service is working fine.

RestEasy Client – ResteasyClient example

As I mentioned earlier, RestEasy also provides API for testing rest web services through java program. For this we need to add another dependency in our pom.xml file.

Below is the simple test program where I am testing our web service programmatically. I am providing example for testing GET, POST and DELETE HTTP methods.

Below is the output produced by above RESTEasy client program.

That’s all for RESTEasy tutorial. RESTEasy is a very easy framework for creating Restful web services in java easily. You can download the project from below link and play around with it to learn more.

By admin

Leave a Reply