Playing with REST annotations

In the last article in addition to setting up our IDE for writing REST services we have also got friendly with four basic annotations. In this article we will look in to some of the other commonly used annotations in REST.

The example of greeting a user is used through this article to help you comprehend and assimilate the tips provided in this article. Let us start by creating the form

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     <title>Playing with Annotations</title>
      <form method="post" action="/PracticeREST/rest/helloworld/ ">
         <label>User Name:</label>
         <input type="text" name="name"></input>
         <input type="submit" value="submit"></input>

On submit, the form will be submitted as a POST request to the REST service deployed at PracticeREST/rest/greetings/ (same service that we wrote in our last article with certain modifications). The REST service should welcome the entered user. Pretty similar to the service that we saw in our last article with the following changes,

  1. Have to handle POST request rather than GET as we did previously
  2. Have to get the user name from form

Once we complete this, we will also look in to other ways of getting the user name, for example from URL and HTTPServletRequest object.

Manipulating Form Attributes:

For handling POST request, the POJO method needs to be decorated with @POST annotation as shown below,

public String sayHello(){
	//implement the business logic here

REST provides two annotations @MultivaluedMap and @FormParam to manipulate form attributes.  Using @FormParam(“param name”) we will be able to get the value of a particular form parameter as shown below,

public String sayHello(@FormParam("name") @DefaultValue("NoName") String name ){
	return "Howdy "+name+" !! Welcome to Jersey REST.";

In addition to @FormParam, REST provides @MultivaluedMap annotation to iterate over all the form parameters as shown below,

public String sayHello(MultivaluedMap<String, String> formParams){
	for (String key : formParams.keySet()) {
			formparameters = formparameters + formParams.get(key);
		formparameters += "<br> Value got using FormParam " + userName;

Note: Form parameters that has name attribute can only be manipulated with @MultivaluedMap and @FormParam annotations

Other ways to get user inputs

In addition to the above annotations which directly work on form parameters, REST provides @PathParam, @Context annotation to get user inputs.

Using @PathParam

To illustrate this, let us add a new link to our index.jsp as shown below

<input type="text" name="name"></input>
<a href="/PracticeREST/rest/helloworld/veechand">Click Me</a>

Clicking on the link will direct the user to the service at /PracticeREST/rest/helloworld/veechand as a GET request. Assume that the username in the link, namely veechand, is dynamically populated using the logged in user. In this case our service URL should be something like /PracticeREST/rest/helloworld/[variable] and our business logic should able to get and process on the variable. @PathParam helps us in doing this,

public String sayHello(@PathParam("username") @DefaultValue("NoName") String name ){
		return "Howdy "+name+" !! Welcome to Jersey REST.";

1. @PATH annotation has a variable embedded in it. This variable will be substituted at run time. Variable have to be represented in curly braces ‘{‘
2. Value of the URI variables can be got using @PathParam(’[variable name]’). This annotation could take @DefaultValue to assign a default value to the variable.

Using @Context

@Context can be used for obtaining and processing information about the application deployment and individual request. Using @Context we can get handle to HttpHeaders, Request, SecurityContext, HttpServletConfig, ServletContext, HttpServletRequest, HttpServletResponse and EJB container details like @EJB, @EJBs, @WebServiceRef, @WebServiceRefs, @PersistenceContext, @PersistenceContexts, @PersistenceUnit and @PersistenceUnits. So using @Context a handle to Request object can be obtained and manipulated to get the needed parameters

public String post(@Context HttpServletRequest req) {
          String username = req.getParameter(key)

Hopefully you’ve found this tutorial interesting and this has been useful to you. Feel free to reuse this code elsewhere in your projects and chime in here if you are running into difficulties. Questions? Nice things to say? Criticisms? Hit the comments section and leave me a comment. Happy coding!

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Furl

2 Responses

  1. Good one… I was searching for getting request in Jersey and handling more form parameters.. Your blog gave me the answers.

  2. Very good blog to start with Rest services, I was searching for FormURLEncodedProperties and others types..
    But this gives one stop solution to start..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: