Getting started with RestEasy

RestEasy is the REST framework of JBoss. It can be reached from the address http://. This framework implements the JAX-RS specification and allow to implement RESTful services. The latters can be deployed within any JavaEE Web container, not only the JBoss application server.

In this post, we will describe how to initialize a simple RESTful application with EasyRest. Whereas it seems not to so complicated, there are some details not to forget in order to make it work.

Configuring the project

The simplest way to configure an RestEasy application is to use Maven and define the client as a dependency in the file pom.xml, as described below:

<project xmlns=""






We need to add then the JBoss Maven repository to get the RestEasy dependencies, as described below:

            <id>JBoss repository</id>

Since we want to integrate our project within Eclipse and WTP, we need to add the following configuration regarding build:


We are now ready to start to configure our application.

Configuring the JAX-RS application

The first thing to do is to add a file called javax.servlet.ServletContainerInitializer under the folder WEB-INF/services with the following content


With such approach, the file web.xml can remain empty, as described below, but must be present within your application.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
                 id="JAXRS-RESTEasy" version="3.0">

We can notice that the link gives interesting hints to configure RestEasy 3:

The last thing to do before starting to implement our application classes themselves is to implement the application class. The latter must extend the class Application of RestEasy and define the path of the application, / in our case.

public class RESTEasyApplication extends Application {


Implementing a resource

A resource in JAX-RS simply consists in an annotated class using the annotations of this specification. We need to specify first the path to reach the resource using the annotation Path, as described below:

public class PingResource {

Now this done, we can add methods to serve different HTTP methods. We can leverage annotations to specify the corresponding HTTP method, extend the root path of the resource, inject path and query parameters in the method parameters. Below is the content of a single resource method:

public Response pong(
              @PathParam("pathParameter") String pathParameter,
             @DefaultValue("1000") @QueryParam("queryParameter")
             int queryParameter) {

    String response = "Pong - pathParameter : " + pathParameter
                                 + ", queryParameter : " + queryParameter;

    return Response.status(200).entity(response).build();

In the real world, we dont have such simple content but RESTful services use structured content using format like JSON, XML, YAML, JAX-RS like most REST frameworks provides a way to automatically convert beans to content using tools like Jackson. As we saw when defining dependencies in the file pom.xml, you have the Jackson provider for JAX-RS. We are able to such automatic conversion, as described below:

public class ContactListResource {

    public List<ContactBean> getContacts() {
        // Get contacts from database
        List<ContactBean> contacts = (...)

        return contacts;

    public void addContact(ContactBean contact) {
        // Add contact into database

At this level, we have a RESTful application that we can deploy into Tomcat7 and test.

Deploying into Tomcat 7

We dont describe here how to install a Tomcat 7 server within WTP in preferences (Server > Runtime Environments) and create an instance of such server in the view Servers. However, we need not to forget to check the configuration property in the server instance configuration (reachable from server context menu, entry ), as described below:

After having added the project to the WTP Tomcat 7 server, we can start it.

We can see the following traces during the Tomcat server startup. This shows that our application RESTEasyApplication was deployed.

mars 06, 2015 12:01:43 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFOS: Déploiement du descripteur de configuration (...)/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf/Catalina/localhost/JAXRS-RESTEasy.xml
mars 06, 2015 12:01:43 PM org.apache.catalina.startup.SetContextPropertiesRule begin
AVERTISSEMENT: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:TestResteasy' did not find a matching property.
mars 06, 2015 12:01:43 PM org.jboss.resteasy.spi.ResteasyDeployment 
INFOS: Deploying class resteasy.test.RESTEasyApplication
mars 06, 2015 12:01:43 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFOS: Deployment of configuration descriptor (...)/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf/Catalina/localhost/JAXRS-RESTEasy.xml has finished in 804 ms
mars 06, 2015 12:01:43 PM org.apache.coyote.AbstractProtocol start
INFOS: Starting ProtocolHandler ["http-bio-8080"]
mars 06, 2015 12:01:43 PM org.apache.coyote.AbstractProtocol start
INFOS: Starting ProtocolHandler ["ajp-bio-8009"]
mars 06, 2015 12:01:43 PM org.apache.catalina.startup.Catalina start
INFOS: Server startup in 1304 ms

Testing your RESTful application

There are several HTTP clients that we can use to test a RESTful application. Most famous are curl with command line and Postman in Chrome if we want a friendly user interface.

Here are the curl commands to interact with our service:

curl -X GET http://localhost:8080/JAXRS-RESTEasy/ping?pathParameter=path&queryParameter=12
Pong - pathParameter: path - queryParameter: 12

curl -X POST -d '{"firstName":"first name", "lastName":"last name"}' -H "Content-Type: application/json" http://localhost:8080/JAXRS-RESTEasy/contacts

This entry was posted in REST, RestEasy and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s