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.
- 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
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.
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.
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/application.properties file.
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.
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.
We will handle errors specific to our application. Create a class called “ErrorConstants.java” where we will define some constants that we will use in our application.
Similarly create a class “ErrorResponse.java” 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 EmployeeController.java 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 “EmployeeConfig.java” 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.
“SpringBootJerseyIntegrationApplication.java” 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 “SpringBootJerseyIntegrationApplication.java” 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.