Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

JAX-RS Annotations


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


Click me to download jersey jar files.


JAX-RS Annotations

The javax.ws.rs package contains JAX-RS annotations.

Annotation Description
Path It identifies the URI path. It can be specified on class or method.
PathParam represents the parameter of the URI path.
GET specifies method responds to GET request.
POST specifies method responds to POST request.
PUT specifies method responds to PUT request.
HEAD specifies method responds to HEAD request.
DELETE specifies method responds to DELETE request.
OPTIONS specifies method responds to OPTIONS request.
FormParam represents the parameter of the form.
QueryParam represents the parameter of the query string of an URL.
HeaderParam represents the parameter of the header.
CookieParam represents the parameter of the cookie.
Produces defines 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.
Consumes It 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.Kreationnext.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.Kreationnext.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/Kreationnext”>Click Here</a>

Now run this application on server, you will see the following output:

Output:

Jersey say : Kreationnext 

Click me to download this example


JAX-RS Multiple @PathParam Annotation

File: HelloService.java

  1. package com.Kreationnext.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.        String date = year + “/” + month + “/” + day;
  15.        return Response.status(200)
  16.         .entity(“getDate is called, year/month/day : “ + date)
  17.         .build();
  18.     }
  19.  }

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

Click me to download this example


JAX-RS @FormParam and @POST Annotation

File: HelloService.java

  1. package com.Kreationnext.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.         return Response.status(200)
  15.             .entity(” Product added successfuly!<br> Id: “+id+“<br> Name: “ + name+“<br> Price: “+price)
  16.             .build();
  17.     }
  18. }

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-param1

jax-rs-form-param2

Comments

comments