Sunday, 25 October 2015

Simple REST service that provides CRUD operations to clients using java Restlet API

This tutorial demonstrates how to create a REST service application in Eclipse IDE using the java Restlet API and the database as MYSQL.
This is an example that includes creating a RESTful service which allows clients to use any REST client and CRUD (create, retirieve, update or delete) employee details stored in a MYSQL database. The HTTP methods GET (retrieve), PUT(create), DELETE(delete),POST(update) are supported for this service. The response for all methods would be XML (DOMRepresentation).

Before we start with the application we need the following:

Step 1: Create a database called 'employeedb' using the MySQL workbench and create a table called 'employeeDetails' (which will store the employee ID, first and last names, telephone numbers of employees) and insert some data into the tables.

EmployeeDetails table

Step 2: Create a Java project in Eclipse.

Step 3: Provide a name for your project and click on ‘Finish’

Step 4: Add the following jars (which are present in the downloaded restlet-x.x -> lib -> required folder)  and also the downloaded mysql odbc jar file to the project by right clicking on the project name followed by Properties ->Java Build Path -> Libraries -> Add External jars

  • org.restlet.jar
  • org.restlet.ext.xml.jar
  • mysql-connector-java.xxxx.jar

Step 5: Create 4 packages as follows:

Step 5.1: First package com.myapp.object and create class within as shown below:


Step 5.2: Second package com.myapp.server and create class within as shown below:


Step 5.3: Third package com.myapp.server.dao and create class within as shown below:


Step 5.4: Fourth package com.myapp.server.resource and create class within as shown below:


Step 6: The final directory structure would look as follows:

Step 7: Start the REST service.

Run the (it contains the main method)

Step 8: Using the REST service. 
Any REST client can be used (Firefox REST Client has been used in this example)

Step 8.1: GET Resource

Method:GET, URI:http://localhost:8112/employeeResource/getEmployee/1

Step 8.2: PUT (create/add)Resource

Method:PUT, URI:http://localhost:8112/employeeResource/putEmployee/4,scooby,doo,324234

Step 8.3: POST (update)Resource

Method:POST, URI:http://localhost:8112/employeeResource/updateEmployee/3,fred,flintstone,33234

Step 8.4: DELETE (delete)Resource

Method:DELETE, URI:http://localhost:8112/employeeResource/deleteEmployee/3

  • The EmployeeDAO would vary depending on the database or the way server accesses the data and must be updated with the appropriate login credentials.
  • The REST service (step 4 completion) can be started on any port. In this example port 8112 has been used. The details of the port or the URL's to be allowed for various resources are given in com/myapp/server/
  • The Restlet version used in this sample is 2.3.5
  • The response for all methods in com/myapp/server/resource/ is a Representation. DomRepresentation is being used in this example as XML is usually the standard response and is easy to parse. However, any subclass of  org.restlet.representation.Representation can be used(