Annotations in Rest Webservices

1) @Path() Annotation:

-Its a Class & Method level of annotation
-This will check the path next to the base URL
Base URL :
http://localhost:(port)/<YourApplicationName>/<UrlPattern In Web.xml>/<path>

2)@GET
Its a method level of annotation,responds to HTTP GET request only.

3)@POST
Its a method level of annotation,responds to HTTP POST request only.

4)@PUT
Its a method level of annotation,responds to HTTP PUT request only.

Note: @POST and @PUT are same.@PUT is mostly used to update a entity(entity represents a table in a relational database) while @post is used to create a entity.

5)@Produces
When we invoke web service, it produces output in different formats like xml, json etc.
Using @Produces we specify output should be produced in which format.

eg.
@Produces("application/xml,application/json")
So here o/p will be produced in xml or json

Note: We use@Produces annotation for GET requests only.
Note:By default the "text/html" media type is returned.

6)@Consumes
It defines in which format the method can accept the input from the client.
Note:By default the "text/html" media type is returned.


7)@PathParam
This annotation will be used to bind RESTful URL parameter values to the method arguments.
For example in an url of the form
http://example.com/books/{bookid}, you can use @PathParam("bookid") to get the id of a book.
eg.
@Path("/{bookid}")
public return_type methodName(@PathParam("bookid") int cusNo) throws ClassNotFoundException, SQLException {}


8)@QueryParam
@QueryParam is used to access key/value pairs in the query string of the URL (the part after the ?).
For example in the url
http://example.com?q=searchterm, you can use @QueryParam("q") to get the value of q.
But in case of @PathParam we will get parameter values directly.

eg.
Consider this URL:
http://localhost:8080/RestPathAnnotationExample/rest/customers?name=Rahul&country=India
Here query parameters are name, country and their values are Rahul, India respectively.

 
9)@MatrixParam
Same as @QueryParam. Only difference is it is seperated by semicolon(;)

eg.
http://localhost:8080/<projectRoot>/rest/customers;name=rahul;country=India


10)@FormParam annotation
By using @FormParam annotation, RESTful web service would accept HTML form parameters sent by the client in the POST request and bind them to the method variables.

1 comment: