Saturday, 26 September 2015

RESTful JAX-RS Annotations Example

RESTful JAX-RS Annotations Example



JAX-RS API provides following annotations to develop RESTful applications in java. We are using jersey implementation for developing JAX-RS examples.



JAX-RS Annotations

The javax.ws.rs package contains JAX-RS annotations.
AnnotationDescription
PathIt identifies the URI path. It can be specified on class or method.
PathParamrepresents the parameter of the URI path.
GETspecifies method responds to GET request.
POSTspecifies method responds to POST request.
PUTspecifies method responds to PUT request.
HEADspecifies method responds to HEAD request.
DELETEspecifies method responds to DELETE request.
OPTIONSspecifies method responds to OPTIONS request.
FormParamrepresents the parameter of the form.
QueryParamrepresents the parameter of the query string of an URL.
HeaderParamrepresents the parameter of the header.
CookieParamrepresents the parameter of the cookie.
Producesdefines media type for the response such as XML, PLAIN, JSON etc. It defines the media type that the methods of a resource class or MessageBodyWriter can produce.
ConsumesIt defines the media type that the methods of a resource class or MessageBodyReader can produce.

JAX-RS @Path, @GET and @PathParam Annotations

File: HelloService.java
  1. package com.javatpoint.rest;  
  2. import javax.ws.rs.GET;  
  3. import javax.ws.rs.Path;  
  4. import javax.ws.rs.PathParam;  
  5. import javax.ws.rs.core.Response;  
  6. @Path("/hello")  
  7. public class HelloService{  
  8.     @GET  
  9.     @Path("/{param}")  
  10.     public Response getMsg(@PathParam("param") String msg) {  
  11.         String output = "Jersey say : " + msg;  
  12.         return Response.status(200).entity(output).build();  
  13.     }  
  14. }  
File: web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3. xmlns="http://java.sun.com/xml/ns/javaee"   
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"   
  6. id="WebApp_ID" version="3.0">  
  7.  <servlet>  
  8.     <servlet-name>Jersey REST Service</servlet-name>  
  9.     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
  10.     <init-param>  
  11.         <param-name>jersey.config.server.provider.packages</param-name>  
  12.         <param-value>com.javatpoint.rest</param-value>  
  13.     </init-param>  
  14.     <load-on-startup>1</load-on-startup>  
  15.   </servlet>  
  16.   <servlet-mapping>  
  17.     <servlet-name>Jersey REST Service</servlet-name>  
  18.     <url-pattern>/rest/*</url-pattern>  
  19.   </servlet-mapping>  
  20. </web-app>   
File: index.html
  1. <a href="rest/hello/javatpoint">Click Here</a>  
Now run this application on server, you will see the following output:
Output:
Jersey say : javatbeginnerstutorial 


JAX-RS Multiple @PathParam Annotation

File: HelloService.java
  1. package com.javatpoint.rest;  
  2. import javax.ws.rs.GET;  
  3. import javax.ws.rs.Path;  
  4. import javax.ws.rs.PathParam;  
  5. import javax.ws.rs.core.Response;  
  6. @Path("/hello")  
  7. public class HelloService{  
  8.     @GET  
  9.     @Path("{year}/{month}/{day}")  
  10.     public Response getDate(  
  11.             @PathParam("year"int year,  
  12.             @PathParam("month"int month,   
  13.             @PathParam("day"int day) {  
  14.    
  15.        String date = year + "/" + month + "/" + day;  
  16.    
  17.        return Response.status(200)  
  18.         .entity("getDate is called, year/month/day : " + date)  
  19.         .build();  
  20.     }  
  21.  }  
File: web.xml
It is same as above example.
File: index.html
  1. <a href="rest/hello/2014/12/05">Click Here</a>  
Now run this application on server, you will see the following output:
Output:
getDate is called, year/month/day : 2014/12/5


JAX-RS @FormParam and @POST Annotation

File: HelloService.java
  1. package com.javatpoint.rest;  
  2. import javax.ws.rs.FormParam;  
  3. import javax.ws.rs.POST;  
  4. import javax.ws.rs.Path;  
  5. import javax.ws.rs.core.Response;  
  6. @Path("/product")  
  7. public class ProductService{  
  8.     @POST  
  9.     @Path("/add")  
  10.     public Response addUser(  
  11.         @FormParam("id"int id,  
  12.         @FormParam("name") String name,  
  13.         @FormParam("price"float price) {  
  14.    
  15.         return Response.status(200)  
  16.             .entity(" Product added successfuly!<br> Id: "+id+"<br> Name: " + name+"<br> Price: "+price)  
  17.             .build();  
  18.     }  
  19. }  
File: web.xml
It is same as above example.
File: index.html
  1. <form action="rest/product/add" method="post">  
  2. Enter Id:<input type="text" name="id"/><br/><br/>  
  3. Enter Name:<input type="text" name="name"/><br/><br/>  
  4. Enter Price:<input type="text" name="price"/><br/><br/>  
  5. <input type="submit" value="Add Product"/>  
  6. </form>  
Now run this application on server, you will see the following output:
Output:
jax rs form param example
jax rs annotation form param example

No comments:

Post a Comment