Thursday, June 11, 2015

Restful Web Service Parameters

@PathParam

The @PathParam annotation binds the value of a path segment to a resource method parameter. For example, the following method would intercept an HTTP GET like http://server:port/login/12345 and convert the PathParam "12345" into the String "id"

@Path("/")
public class LoginService
{
  @GET
  @Path("login/{zip}")
  public String login(@PathParam("zip") String id) {
   return "Id is " +id;
  }
}
As for @FormParam, you can embed the @PathParam declaration at class level, if you prefer.

@QueryParam

The @QueryParam annotation binds the value of a path segment to a resource method parameter. For example, the following method would intercept an HTTP GET like http://server:port/login?zip=12345 and inject the query parameter "zip" into the method parameter "zip"

@Path("/")
public class LoginService
{
 @GET
 @Path("login/{zip}")
  public String login(@QueryParam("zip") String zip) {
   return "Id is " +id;
  }
}
QueryParam can be convenientely used with the DefaultValue annotation so that you can avoid a null pointer exception if no query parameter is passed.

@GET
@Path("login/{zip}")
public String login(@DefaultValue("11111") @QueryParam("zip") String zip) {
 return "Id is " +id;
 }
As for @FormParam, you can embed the @PathParam declaration at class level, if you prefer.

@HeaderParam

The @HeaderParam annotation extracts information from the HTTP header and binds it to a method parameter. Example:

@GET
public String callService(@HeaderParam("User-Agent") String whichBrowser) {
...
}

@CookieParam

The @CookieParam annotation reads an information stored as a cookie and binds it to a method parameter. Example:

@GET
public String callService(@CookieParam("sessionid") String sessionid) {

...
 }

@MatrixParam

The @MatrixParam annotation can be used to bind an expression containing several property=value to a method parameter. For example, supposing you were to invoke an URL like http://server:port/login;name=francesco;surname=marchioni

@GET
public String callService(@MatrixParam("name") String name,
                                @MatrixParam("surname") String surname) {
...
 }

Reference :- http://www.mastertheboss.com/jboss-frameworks/resteasy/resteasy-tutorial-part-two-web-parameters