Tuesday 1 December 2015

BUILDING JAVA APPLICATION USING REST API AND MAVEN

JAVA Api for RESTful webservices (JAX-RS) makes developer to develop webservices application easily. In this blog, I am going to tell you how to build a webservice application using JAX-RS. After going through this blog, you will get a head start on JAX-RS and also get an idea about Jersey.
In this tutorial, I have used Eclipse luna, Apache Tomcat and maven. Please see my previous blog to do the server set up.
I have created a simple Employee crud application where using webservice we can get all the details of employees, delete a employee record based on employeeId and also get a employee record based on employeeId.
Instead of using database, I have simply created a static list of some employees.
Please follow my pom.xml and add all the dependencies in your pom.xml as we are creating MAVEN project here.
POM.XML




WEB.XML



So according to the web.xml, any request which comes through /rest/{"anything"}, JAX-RS will try to map the correct services class which are having @PATH annotations and  executes the code.

Model Class( Employee.java)
Please note that here we are sending a JSON response, so we need to provide @XmlRootElement so that jax-rs can identify what kind of response we will send .





Model Class(Address.java)

We are following Service Oriented Architecture(SOA) to develop our JAX-RX application where we have services layer and business layer. Our JAX-RX application looks like:




Singleton Class(EmpSingleton.java):
We have written the Singleton class just to ensure that in our application there is only one EmpSingleton instance. It restricts to create multiple objects of EmpSingleton class.



Business Layer(EmpService.java): This is our business layer. All business specific rule should be implemented here. We have three methods here.
getAllEmployees(): This method returns all the employees present in List or Database.
getDetailsonEmpId(Long id): It returns the details of a particular employee whose id has been provided as a parameter.
deleteEmployeeonId(Long id): It deletes the employee record from the list or database whose id has been passed as parameter.


Services Layer(EmpResource.java): This is our services layer. This layer is responsible to call business layer. If User provides a url like: http://localhost:8080/projectName/rest/emp, then JAX-RS will try to map the url within our aplication. Here @Path annotation is used to map the url and identify the correct class. @Produces annotation tells that response will be JSON types. 


So we have written our code. The next step is to deploy the code into Tomcat server. Just right click on the project and run it on server. Once you deploy it on server , just download the Google Advanced Rest Api Client and install it. Once you install it on chrome, you can see the api like:



Just paste the URI and click on send. This is a very helpful plugin to test JSON/XML request-response on JAX-RS. We are sending here GET request, so according to the URL we should get all the employee details.



Like this, we can send PUT,POST,DETELE request and based on URL, the method in services layer will get fired. In the following URL, we are sending /emp/1 as GET request , where 1 is a empId, so it will return the json response of Employee having empId=1.
Request.


Response:


That is all guys. I will discuss more about annotations in my next post.
I believe this will surely give a brief knowledge about JAX-RX. I would love to hear feedback from you. Thank you!!.