RESTful CRUD operations using Jersey and Hibernate

Here are the objectives of this article :
– Use Maven to create a Jersey JAX-RS service
– Modify the service class to provide GET, POST, UPDATE,DELETE operations
– Use Hibernate to connect to the MySQL database and perform the CRUD operations



  • Jersey
  • Hibernate
  • Maven
  • MySQL database
  • Eclipse

Here is an overview of the steps required:

  • Create a REST service using Maven archetype

  • Add methods for GET, POST, DELETE in the service class

  • Create DAO layer

  • Deploy and Test


Step 1: (Create a simple RESTful service using Maven)

To create a RESTful service using maven, run the following archetype command :

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.topjavatutorial -DartifactId=JerseyHibernateApp -Dpackage=com.topjavatutorial -DarchetypeVersion=2.22.1

Import the project in Eclipse. Deploy it in Tomcat server and test it to make sure it’s working as expected.
Refer the detailed steps for creating the JAX-RS Hello World service here :


Creating RESTful webservice using Jersey

Now that we are ready with the basic project, let’s work on adding all the operations.


Step 2: (Modify the MyResource class)

The maven command created a com.topjavatutorial package inside src/main/java.
This package contains the MyResource class, that contains implementation of a simple JAX-RS resource.
Let’s modify the MyResource class to handle GET, POST, DELETE and UPDATE requests:

package com.topjavatutorial;

import java.util.List;


import com.topjavatutorial.dao.Employee;
import com.topjavatutorial.dao.EmployeeDAO;

public class MyResource {

    public List<Employee> getEmployee() {
      EmployeeDAO dao = new EmployeeDAO();
      List employees = dao.getEmployees();
        return employees;

    public Response addEmployee(Employee emp){
    EmployeeDAO dao = new EmployeeDAO();
    return Response.ok().build();
    public Response updateEmployee(@PathParam("id") int id, Employee emp){
      EmployeeDAO dao = new EmployeeDAO();
    int count = dao.updateEmployee(id, emp);
      return Response.status(Response.Status.BAD_REQUEST).build();
    return Response.ok().build();
    public Response deleteEmployee(@PathParam("id") int id){
      EmployeeDAO dao = new EmployeeDAO();
    int count = dao.deleteEmployee(id);
      return Response.status(Response.Status.BAD_REQUEST).build();
    return Response.ok().build();


Step 3: (Implement the DAO layer)

In the MyResource class, we are delegating on the db operations to an EmployeeDAO class.
We can implement this EmployeeDAO class using plain JDBC, or ORM tools like Hibernate, iBatis etc.
Here is the implementation for Hibernate integration, to implement the EmployeeDAO class and other required classes like hibernate.cfg.xml, SessionUtil and Employee entity class.
Adding HIbernare DAO layer for Jersey service

Step 4: (Deploy and Test)

Here is how the project structure looks like at this point:
Jersey Hibernate project structure
Now, lets deploy the project in Tomcat server and submit the following url in browser:
We currently haven’t added any records in Employee table. But if we added any records in Employee table, we should get a JSON response like this in the browser:
[{“age”:21,”id”:1,”name”:”John Doe”}]
We can invoke the GET request using the browser, but we need an utility like POSTMAN or a custom REST client to test the other methods.

Testing using Postman extension in Chrome

We are using a chrome app called Postman to test the REST apis. You can search for postman extension in chrome to install the same.

Here are some screenshots of using Postman to test these apis :

Test Add Employee
REST POST example

To verify that employee is added, we can use the GET operation :
REST GET Example
Test Update Employee
REST PUT Example
To verify that employee is updated, we can use the GET operation :
Test Delete Employee
REST Delete Example
To verify that employee is deleted, we can use the GET operation :
REST GET employee

Testing using Rest Client

Here is the Rest client implementation using Jersey to test the POST, GET and DELETE operations:
REST client using Jersey
We can use this Jersey client implementation to test all the service methods added here.

You may also like following REST client implementations using HttpClient api:

REST client using HttpClient 3 api 
REST client using HttpClient 4 api

Further Reading



You may also like :



© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post


  1. i did not understand in step 1, how to run the following archetype command?

  2. Thanks for the amazing tutorial.Can i download the source code from somewhere ?

  3. […] Restful webservice with CRUD operations using Jersey and Hibernate […]

  4. […] This client will be invoking a service that is deployed locally on Tomcat server.     We created the RESTful Service created using Jersey and Hibernate in this article : RESTful service using Jersey and Hibernate […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: