Thursday 5 May 2016

Different annotation in restful webservices

In this tutorial, we are going to discuss different annotations in RESTful webservices in depth. Every annotation has their own meaning.


@GET: Get is used to get some content in XML or JSON format.

  • @GET
  • public String getDetails()
  • {
  • .......
  • }


@PRODUCES: It specifies the type of output method will generate. A method can generate XML or JSON output.

@CONSUMES: It specifies the MIME type that our web service can consume.

  • @GET
  • @PRODUCES("application/xml")
  • @CONSUMES("application/xml")
  • public Person getPersonDetailsInXML()
  • {
  • ........
  • }


  • @GET
  • @PRODUCES("application/json")
  • @CONSUMES("application/json")
  • public Person getPersonDetailsInJSON()
  • {
  • ......
  • }


@PATH: It specifies the URL path on which method is invoked.

  • @GET
  • @PRODUCES("application/json")
  • @CONSUMES("application/json")
  • @PATH("/getPersonDetails")
  • public Person getPersonDetails()
  • {
  • ......
  • }


@PATHPARAM: PathParam annotation is used to bind some value with argument so that we can get dynamic result. For example we want to fetch the person details based on person id. 

  • @GET
  • @PRODUCES("application/json")
  • @CONSUMES("application/json")
  • @PATH("/getPersonDetails/{personId}")
  • public Person getPersonDetails(@PathParam("personId") String personId)
  • {
  • .....
  • }

@POST: Post annotation is used to insert data. For example if we want to create a new Person record then we should use @POST .


  • @POST
  • @PRODUCES("application/json")
  • @CONSUMES("application/json")
  • @PATH("/createPerson")
  • public Person createPerson(@RequestBody Person person)
  • {
  • ......
  • }


@RequestBody annotation is used to bind a Object.

@PUT: Put is used when we need to update the data which is already there in DB.


  • @PUT
  • @PRODUCES("application/json)
  • @CONSUMES("application/json")
  • @PATH("/editPerson")
  • public Person editPersonDetails(@RequestBody Person person)
  • {
  • .........
  • }


@DELETE: It specifies the delete operation based on path variable. For example we will delete the person object based on personId.


  • @DELETE
  • @PRODUCES("application/json")
  • @CONSUMES("application/json")
  • @PATH("/deletePerson/{personId}")
  • public Person deletePerson(@PathParam("personId") String personId)
  • {
  • ......
  • }


Guys, these are some of basic annotations which are used frequently in restful application. Please keep in mind that PUT is used to update the data and POST is used to create a fresh data in DB. Many people get confused between PUT and POST.

Hope this article will help you. I would love to hear feedback. Thank you.