Friday, January 31, 2014

Implementation of Restful Webservice with image

In Java EE 6, JAX-RS provides the functionality for Representational State Transfer (RESTful) web services. REST is well suited for basic, ad hoc integration scenarios. RESTful web services, often better integrated with HTTP than SOAP-based services are, do not require XML messages or WSDL service–API definitions. Before proceeding please read this article, Developing RESTful Web Services with JAX-RS.

Also please have look at the tutorial about "JAXB marshalling and unmarshalling", as it would be helpful to understand Restful Webservice.

For running this example, below mentioned particulars are used.
  • Tomcat 7.0.11
  • JDK 1.5
  • MyEclipse 8.6.1
1. Create a new Web Service Project, File -> New -> Web Service Project

2. Provide the project name as "RestfulWebserviceExample". Choose framework as REST()JAX-RS) and click Next

3. Keep default setting of URL pattern as "/services/*" however you may change it as per your requirement. Also keep the default JAX-RS library as shown in the below figure.

4. You can find below web.xml generated automatically. Instead of creating a new project, you may also add REST capabilities to an existing Java web project  by right click on project -> MyEclipse -> Add REST Capabilities...


5. We will start with the two simple POJO classes and, as shown in "JAXB marshalling and unmarshalling" example. Employee and Name has a HAS-A relationship.

 6. Now we will create a New Web Service i.e. which is the core of our Restful webservice example.

Choose framework as REST()JAX-RS), select Create new Java bean and click Next

  • URL path is the @Path annotation's value which is a relative URI path.
  • Singleton lifecycle ensures that only one instance of this class will created by Jersey ServletContainer.
  • The @Produces annotation is used to specify the MIME media types a resource can produce and send back to the client.
  • The @Consumes annotation is used to specify the MIME media types a resource can consume that were sent by the client.

7. You can add Methods by clicking the Add button as shown below.

We will create below methods
  1. getEmployeeList() - which will show all the available employees
  2. getEmployee() - For showing data of one particular employee
  3. addEmployee() - to add employee in the list
  4. deleteEmployee() - to delete a employee from the list
getEmployeeList() - The HTTP method dropdown can be used to specify what type of HTTP request this method will respond to; in this case, we wish to respond to an HTTP GET request. Provide its return type i.e. List of Employees. You can verify using Method Signature preview.

Alternatively you can also Add/Edit a class by (right click on the class) -> MyEclipse -> Add REST Method...

getEmployee() - To obtain the value of the empl Id, the @PathParam annotation may be used on the method parameter of a request method as shown below.

addEmployee() - In this case, HTTP method is of POST type. It consumes application/xml as Input and overrides Produces of EmployeeService class as text/plain as a output.

deleteEmployee() - Similarly we have a DELETE method.

List of generated methods as shown below.

8. Click finish. You will find unimplemented methods as shown below. You may need to implement business logics as per your requirements.

Implementing methods - Lets implement the method with our business logic. We are hardcoding a employee in our constructor. In practical you may use Hibernate and Store the employee in your database. Please see the self explanatory example below.

9. Deploy the project as any other web project

10. Start the tomcat. You can see the constructor called by the Jersey servlet container.

12. Hit the below URL. To show the list of employees


URL for a single employee. Just append the employee Id at the end as shown below.


For making a POST request you can use Mozilla poster addon. After installing Poster addon you find it by clicking Tools -> Poster

URL for adding an employee


Add the sample XML and click on POST
<employee emplId="002"><department>HR</department><name><firstName>Rahul</firstName><lastName>Sharma</lastName></name></employee>

Now if you hit the URL to fetch the list of employees you will find 2 employees

Same way you can delete an employee.

Delete URL


After delete, now only one employee left in the list

This example is to show the eclipse implementation of restful webservice. To know more, please read this article, Developing RESTful Web Services with JAX-RS.

No comments:

Post a Comment