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
Tools/Technologies:
- 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 javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import com.topjavatutorial.dao.Employee; import com.topjavatutorial.dao.EmployeeDAO; @Path("/employees") public class MyResource { @GET @Produces("application/json") public List<Employee> getEmployee() { EmployeeDAO dao = new EmployeeDAO(); List employees = dao.getEmployees(); return employees; } @POST @Path("/create") @Consumes("application/json") public Response addEmployee(Employee emp){ emp.setName(emp.getName()); emp.setAge(emp.getAge()); EmployeeDAO dao = new EmployeeDAO(); dao.addEmployee(emp); return Response.ok().build(); } @PUT @Path("/update/{id}") @Consumes("application/json") public Response updateEmployee(@PathParam("id") int id, Employee emp){ EmployeeDAO dao = new EmployeeDAO(); int count = dao.updateEmployee(id, emp); if(count==0){ return Response.status(Response.Status.BAD_REQUEST).build(); } return Response.ok().build(); } @DELETE @Path("/delete/{id}") @Consumes("application/json") public Response deleteEmployee(@PathParam("id") int id){ EmployeeDAO dao = new EmployeeDAO(); int count = dao.deleteEmployee(id); if(count==0){ 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:
Now, lets deploy the project in Tomcat server and submit the following url in browser:
http://localhost:8080/JerseyHibernateApp/webapi/employees
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 :
To verify that employee is added, we can use the GET operation :
Test Update Employee
To verify that employee is updated, we can use the GET operation :
Test Delete Employee
To verify that employee is deleted, we can use the GET operation :
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
- JAX-RS HTTP Method Annotations
- REST architectural principles
- Standalone Restful service using Restlet framework
You may also like :
- Hello World Programs in Different Languages and Frameworks
- Best 25 Java articles on the web in 2015 (Worth Reading !!)
- March 2016 Magazine : TopJavaTutorial articles
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post
#
i did not understand in step 1, how to run the following archetype command?
#
Thanks for the amazing tutorial.Can i download the source code from somewhere ?
#
#