JAX-RS -Annotations
We have many annotations. But below are the majorly used annotations in RESTFul webservices
-
@Path(‘Path‘)
-
@GET
-
@POST
-
@PUT
-
@DELETE
-
@Produces(MediaType.TEXT_PLAIN [, more-types])
-
@Consumes(type[, more-types])
-
@PathParam()
-
@QueryParam()
-
@MatrixParam()
-
@FormParam()
1.@Path()
-
Its a Class & Method level of annotation
-
This will check the path next to the base URL
Syntax: http://localhost:(port)/<YourApplicationName>/<UrlPattern In Web.xml>/<path>
Here <path>
is the part of URI, and this will be identified by @path
annotation at class/method level.
2.@GET
Its a method level of annotation, this annotation indicates that the following
method should respond to the HTTP GET request only, if we annotate our method
with @GET, the execution flow will enter that following method if we send GET
request from the client
3.@POST
It’s a method level of annotation, this annotation indicates that the following
method should respond to the HTTP POST request only.
4.@PUT
It’s a method level of annotation, this annotation indicates that the following
method should respond to the HTTP PUT request only.
5.@DELETE
It’s a method level of annotation, this annotation indicates that the following
method should respond to the HTTP DELETE request only.
6.@Produces
It’s a method or field level annotation, this tells which MIME type is
delivered by the method annotated with @GET. Whenever we send a HTTP GET
request to our RESTful service, it will invokes particular method and produces
the output in different formats. There you can specifies in what are all formats
(MIME) your method can produce the output, by using @produces annotation.
Remember: We will use @Produces annotation for GET requests only.
7.@Consumes
This is a class and method level annotation; this will define which MIME type is
consumed by the particular method. It means in which format the method can
accept the input from the client.
@PathParam, @QueryParam, @MatrixParam annotations will come into picture in case if we are passing the input values to the restful service through the URL
8.@PathParam
http://localhost:8001/<Rest Service Name>/rest/customers/100/Satya
Here the two parameters appear in the end of the above URL [100 & Satya], which are separated by forward slash (/) are called as path parameters
9.@QueryParam
http://localhost:8001/…/rest/customers?custNo=100&custName=Satya
If the client sends an input in the form of query string in the URL, then those parameters are called as Query Parameters. If you observe the above syntax, client passing custNo=100&custName=Satya started after question mark (?) symbol and each parameter is separated by & symbol, those parameters are called as query parameters.
10.@MatrixParam
http://localhost:8001/…/rest/customers;custNo=100;custName=Satya
Matrix parameters are another way defining the parameters to be added to URL. If you observe the above syntax, client is passing two parameters each are separated by semicolon (;), these parameters are called as matrix parameters. Remember these parameters may appear any where in the path.
11.@ FormParam
If we have a HTML form having two input fields and submit button. Lets client
enter those details and submit to the RESTful web service. Then the rest service
will extract those details by using this @FormParam annotation.