Jersey 2 CRUD example

Jersey 2 CRUD example

Jersey is very popular framework to build restful services. In our previous blog Jersey 2 Hello World Example, we came up with basic example to get start with jersey framework. In this blog we will be discussing CRUD operation with jersey which will help you to create restful services with back end data. Technologies used in this examples are :

  1. Maven as a build tool.
  2. Jersey 2.23.1
  3. JDK 1.7
  4. Tomcat 7
  5. Spring 4.1, however you can use lower versions of spring as well.
  6. Mysql as a database

This example has been implemented using Eclipse and maven plugin, used to create the basic folder structure for maven java project. Few other files have been included to implement this example. The final folder structure looks like the image below.


1) Jersey 2 and Spring JDBCTemplate dependencies.

Add Jersey 2, Spring JDBC and mysql dependencies into the pom.xml file.

For table creation, bean class and all other JDBCTemplate code, please refer and follow the steps in spring JDBCTemplate example.

2) Add below servlet mapping configuration into web.xml

3) Rest service class.

Create Rest service class which contains code for CRUD application. Below are description of each methods.

Method NameDescription
update(Emp emp)Update Emp data(JSON) to database.
getById(int id)Returns specific Emp based on id in JSON format.
getAll()Returns all emp from Emp table in JSON format.
save(Emp emp)Insert Emp data(JSON) to database.
deleteById(int id)Delete specific Emp based on id.

4) Run the application.

To run this application, we need rest client. We are using Chrome extension ‘postman’, however you can use any other application or can write your own code to consume these services.

  1. Get all Emp.

    URL : http://localhost:8080/jerseyjdbctemplate/rest/emp/getAll

  2. Get Emp by Id.

    URL: http://localhost:8080/jerseyjdbctemplate/rest/emp/get/1

  3. Insert new Emp.

    URL: http://localhost:8080/jerseyjdbctemplate/rest/emp/save

    Input data in JSON format:

    To verify insert operation, getAll Emp data using URL: http://localhost:8080/jerseyjdbctemplate/rest/emp/getAll

    Jersey 2 CRUD Example - Save

  4. Update Emp

    URL: http://localhost:8080/jerseyjdbctemplate/rest/emp/save
    Input data in JSON format:

    To verify update operation, getAll Emp data using URL: http://localhost:8080/jerseyjdbctemplate/rest/emp/getAllJersey 2 CRUD Example - Update

  5. Delete specific Emp

    URL: http://localhost:8080/jerseyjdbctemplate/rest/emp/delete/3.
    Data after delete operation.

Download the attached zip project.


Stay tuned for more update !!!



Leave a Reply

Your email address will not be published. Required fields are marked *