Building Restful Webservices With Jersey And Spring Boot

Code-Adda: Building Restful webservice With Jersey And Spring Boot

In this article we are going to see how to develop restful web service using JAX-RS specification along with Spring Boot.  This will be a step-by-step tutorial using Jersey implementation combined with the power of Spring Boot + DevTools + JPA + Lombok + H2 in-memory database to demonstrate CRUD restful API. Spring Boot provides the support for Jersey integration with the following spring-boot-starter-jersey dependency in your pom.xml.

[su_box title=”Spring Boot Starter Jersey Dependency” box_color=”#faf62b” title_color=”#151111″]

Technologies Used

  • Spring Boot 2.1.1.RELEASE
  • Jersey 2.27
  • Maven 3.3
  • H2 In-Memory database
  • Eclipse Oxygen

What you will learn?

  • JAX-RS Annotations
  • H2 In-Memory database integration with Spring Boot
  • CRUD Restful API development with Jersey + Spring Boot

JAX-RS Annotations

Before jumping into the actual development, it would be beneficial to know the basics of few jax-rs annotations that we are going to use in our application.

[su_box title=”JAX-RS Annotations” box_color=”#faf62b” title_color=”#151111″]

  • @Path: It specifies the relative URI path to a resource. Can be used at class or method level.
  • @GET: Used at method level that specifies method responds to HTTP GET Request.
  • @POST: Used at method level that specifies method responds to HTTP POST Request. Basically used  to create a new resource.
  • @PUT: Used at method level that specifies method responds to HTTP PUT Request. Use this to update    your resource.
  • @DELETE: Used at method level that specifies method responds to HTTP DELETE Request. Use this to    delete the resourse.
  • @Produces: Used to define media type or response format that resourse method will produce such as XML, PLAIN TEXT, JSON etc.
  • @Consumes: Used to specify the media type that the resource method can accept and process.
  • @ApplicationPath: Used to define URL mapping for all resources. The path specified by @ApplicationPath is the base URI for all resource URIs specified by @Path annotations in the resource class. Basically used on the configuration class i.e. the subclass of (eg. ResourceConfig in our case). Here we will register our class component (EmployeeController.class).
  • @PathParam: It is used to specify the URI path parameters that are extrated from a request URI path template specified in the @Path annotation. A URI template is a URI-like String that contains variables enclosed by braces {}.

H2 In-Memory database integration with Spring Boot

H2 is a popular, open-source and relational database which is written in java. Spring Boot provides out-of-box support for H2. Just add H2 database jar as dependency in your pom.xml and you are ready to use it.

[su_box title=”H2 Dependency” box_color=”#faf62b” title_color=”#151111″]

CRUD Restful API development with Jersey + Spring Boot

Following is the step-by-step guide for the development of your restful API.

Bootstrap you application through SPRING INITIALIZR

As usual, we will create skeleton project from Spring Initializr. Provide the Project Metadata and Dependencies as shown below.

Hit Generate Project to download the project, extract it and import in your favorite IDE. I am using Eclipse Oxygen for the same.

Standard Maven Project Structure

Following is the standard maven project structure in Project Explorer of Eclipse IDE. This is what we are going to develop.

Pom.xml – Project Dependencies
@Entity class Employee

Create a simple Entity class Employee with a primary key empId. The @Id annotation indicates the member field is the primary key of current entity. The @GeneratedValue annotation is to configure the way of increment of the specified column(field). We have used Lombok that generates all the boilerplate associated with your POJOs. @Data is a convenient shortcut annotation that bundles the features of @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together.

Database script file with insert scripts

Create a database script file “data.sql” in “src/main/resources” folder with the following insert scripts that will be loaded in the In-Memory database table “Employee” when the application is started.

Enable H2 Console

If you want to access the web-based H2 console in your browser, then enable the same in the following way in /src/main/resources/ file.

[su_box title=”” box_color=”#faf62b” title_color=”#151111″]
# Enable web-based H2 Console

Now run the project “spring-boot-jersey-integration” as Java Application and you will be able to access the H2 console in your browser with the following url. Remember to change the JDBC url to  jdbc:h2:mem:testdb.

[su_box title=”H2 Console” box_color=”#faf62b” title_color=”#151111″]

Hit the “Connect” button and you will see the “Employee” table already created. Run the select command to see the records for the insert scripts you kept in data.sql file.


JpaRepository is the pre-defined core repository interface in Spring Data JPA enabling the basic CRUD functions on a repository. Create IEmployeeRepository interface extending from JpaRepository that will manage the Employee entity with a primary key of type Integer.

Error Handling

We will handle errors specific to our application. Create a class called “” where we will define some constants that we will use in our application.

Similarly create a class “” that we will use to respond in case resource is not found or in case of failures in our application.

Create a controller class that exposes resources to the web.

Create class annotated with @Component and @Path indicating the URI path for the resource class. @Component is a generic stereotype annotation for any Spring-managed component.

Almost all the annotations used above have already been discussed at the very beginning of this article. Error handling has been done in case resource that is searched for is not found. We also have used several methods given by JpaRepository. Click Me to know all the methods in the JpaRepository. We have also returned different Status code such as ACCEPTED, BAD_REQUEST, CREATED etc as part of our response.

Creating a JerseyConfiguration Class

Create “” jersey configuration class annotated with @Component and @ApplicationPath. The value of @ApplicationPath serves as the base URI for all the resources. Here we have registered our resources class “EmployeeController.class” with register() method.” acts as an entry point for our application. This class is responsible for creating in-memory tomcat container and deploying this application.

Test this REST API using POSTMAN client

Run the above “” as Java Application.

1.) Get all employees
  • Http-Method: GET
  • Request URL: http://localhost:8080/jersey/employee/api/employees

2.) Get an Employee with empId: 103
  • Http-Method: GET
  • Request URL: http://localhost:8080/jersey/employee/api/emp/103

3.) Search for an Employee that never existed
  • Http-Method: GET
  • Request URL: http://localhost:8080/jersey/employee/api/emp/106

4.) Create a resource – Add new employee
  • Http-Method: POST
  • Request URL: http://localhost:8080/jersey/employee/api/addEmp

5.)Update an employee record
  • Http-Method: PUT
  • Request URL: http://localhost:8080/jersey/employee/api/updateEmp/102

6.) Delete an employee record
  • Http-Method: DELETE
  • Request-URL: http://localhost:8080/jersey/employee/api/deleteEmp/1

This is all about jersey and spring boot integration. If you have come so far, i hope you have liked this article. In case you want to know something more, do write us in the comment section.

Further Reading

Integrating H2 In-Memory database with Spring Boot

Be the first to comment

Leave a Reply