Building RESTful Web Services with JAX-RS – Annotations

Post moved to


Let’s take a look at annotations that are used to build RESTful services. Based on the implementation framework that you are using, there might be many annotations. In this post we will discuss annotations that are supported by JAX-RS specs only.


  • @Path annotation identifies URI Path template
  • Can be used at class or method level.
  • Always specifies relative url to base url (host)
  • @Path value need not have leading or tailing slashes (/). JAX-RS treats them them the same either ways
  • Variables can be specified in @path as follows 
    • Multiple Variables can be specified. Eg @Path(“/users/{username_1}/{username_2}”)


//Variable in path is specified by {}
public class UserResource {

    //variable specified in @Path can be accessed by @PathParam
    public String getUser(@PathParam("userName") String userName) {

Request Method Designator Annotations (@GET, @POST, @PUT, @DELETE and @HEAD):

  • @GET – The Java method annotated with this will process HTTP GET requests.
  • @POST – The Java method annotated with this will process HTTP POST requests.
  • @PUT – The Java method annotated with this will process HTTP PUT requests.
  • @DELETE – The Java method annotated with this will process HTTP DELETE requests.
  • @HEAD – The Java method annotated with this will process HTTP HEAD requests.

Few logistics that should be followed to use JAX-RS for request methods designator annotations are as follows

  • Methods decorated with request method designators must return following:
    • void
    • A Java programming language type
    • A Object.
  • Multiple parameters may be extracted from the URI using @PathParam or @QueryParam (Explained below).
  • The HTTP PUT and POST methods expect an HTTP request body.
  • Both @PUT and @POST can be used to create or update a resource.
  • POST can mean anything, so any semantics can be used. PUT has well defined semantics.When using PUT for creation, the client declares the URI for the newly created resource.
  • A common pattern is to use POST to create a resource and return a 201 response with a location header value is the URI to the newly created resource. In this pattern, the web service declares the URI for the newly created resource.

@Consumes and @Produces

@Produces annotation is used to specify the MIME media type that are sent back to client.

  • If specified on class level, all methods will follow it.
  • One can override class level by specifying this on method level.
  • If no methods in a resource are able to produce the MIME type in a client request, then JAX-RS runtime sends back an HTTP ‘406 Not Acceptable’ error.
  • Multiple MIME-types can be specified as follows
      • @Produces({“image/jpeg,image/png”})



public class SomeResource {
    public String doGetAsPlainText() {

    @Produces("text/html") //overides class level
    public String doGetAsHtml() {

@Consumes represents the media types a resource  can accept.

  • If specified on class level, all methods will follow it.
  • One can override class level by specifying this on method level.
  • If a resource is unable to consume the MIME type of a client request, the JAX-RS runtime sends back an HTTP “415 (‘Unsupported Media Type’)” error.
  • If @consumes is used on method that returns ‘void’ then HTTP 204 (‘No Content’) error is returned.

    @POST@Consumes(“text/plain”)public void postClichedMessage(String message) {    // Store the message }

public class SomeResource {
    public String doPost(MimeMultipart mimeMultipartData) {

    public String doPost2(FormURLEncodedProperties formData) {


Request Parameters (@QueryParam, @PathParam, @DefaultValue, @MatrixParam, @HeaderParam, @CookieParam, @FormParam)

Both @QueryParam and @PathParam can be used only on following Java types:

  • All primitive types except char.
  • All wrapper classes of primitive types except Character
  • Any class with a constructor that accepts a single String argument.
  • Any class with static method named valueOf (String) that accepst a single String argument
  • List<T>, Set<T> or SortedSet<T>, where T matches the already listed criteria.


public Response smooth(
        @DefaultValue(&quot;4&quot;) @QueryParam(&quot;number&quot;) int colorNumber,
        @DefaultValue(&quot;red&quot;) @QueryParam(&quot;last-color&quot;) String color
        ) { ... }


Step by Step guide- Hello World REST Service

Post moved to


This post is a step by step guide for a Hello world REST Service using JAX-RS. I am using Eclipse (Mars Edition) and Apache Tomcat for this tutorial. Also I am using Maven for build automation. If you are beginner or  if you have not yet configured your workspace then I recommend these links before reading any further.

Apache Wink is used for JAX-RS implementation for this tutorial.

Step 1: Create a new Dynamic Web Application

Create a new dynamic web application (named HelloWorldRest for this tutorial). Also convert the project into ‘Maven Project’ (This is an optional step if you are planning to use Maven).

Step 2: Update dependencies for Apache Wink

Add these dependencies to pom.xml


If you are not using Maven then download the following jar version into WEB-INF/lib folder.

activation.jar -> 1.1 Version
commons-lang.jar -> 2.3 Version
geronimo annotation_1.1_spec.jar -> 1.0 Version
geronimo-jaxrs_1.1_spec.jar -> 1.0 Version
jaxb-api.jar -> 2.2 Version
jaxb-impl.jar -> Version
slf4j-api.jar -> 1.6.1 Version
stax-api.jar -> 1.0-2 Version
wink-common.jar -> 1.4 Version
wink-server.jar -> 1.4 Version

Step -3: Add code for REST Service

Add below code for HelloWorldResource


package com.test.helloworld.resource;


public class HelloWorldResource {

	 public String getMessage() {
		System.out.println(&quot;Returning Message&quot;);
		return &quot;Hello World!&quot;;

Add below code for HelloWorldApplication


package com.test.helloworld;

import java.util.HashSet;
import java.util.Set;


import com.test.helloworld.resource.HelloWorldResource;

public class HelloWorldApplication extends Application{

	 public Set&lt;Class&lt;?&gt;&gt; getClasses() {
		 Set&lt;Class&lt;?&gt;&gt; classes = new HashSet&lt;Class&lt;?&gt;&gt;();
		 return classes;

Add below entries to web.xml


Step-4: Test HelloWorld REST Service


Step by Step guide – Convert to Maven Project in Eclipse

Post moved to:


This post is for step by step guide to convert a single project to Maven project in Eclipse. I recommend going through these links before you read any further.

Step 1: Maven Plugin for Eclipse

First you should make sure that you have Maven Plugin installed in your Eclipse. I am using Eclipse Mars for this demo and this version comes with Maven Plugin. If you are using Eclipse doesn’t have this built in feature then try to install a Maven Plugin. I recommend M2Eclipse Plugin.

Step 2: Convert Java/J2ee Project to Maven Project

Right click on Java Eclipse and select Configure -> Convert to Maven Project.


A popup up that will show build parameters that will be published in pom.xml will be shown.


Step 3: Add dependencies to pom.xml

Open pom.xml and Click on Dependencies tab.

Lets try to add log4j.jar as a dependency in pom.xml.



Step 4: Run Maven Build

Run Maven build by right clicking on pom.xml and selecting Run As -> Maven install.


Step 5: Verify Build

You will see that folder is created with naming convention <artifactId>-<version> in build folder. Also you will see that all dependencies are saved to build/<artifactId>-<version>/WEB-INF/lib folder. Also you will see all dependecies are placed in build path under Maven Dependencies.




Multiple dependencies can be added in similar fashion.


Step by Step guide for Hello World Page (HTML and JSP)

This post has been moved to


This post is a step by step guide to develop your first html and jsp. Traditionally, developers code ‘Hello World !!!’ as first page and I am sticking to this tradition.

I am using a Eclipse IDE and Apache Tomcat for this tutorial. I recommend going through ‘Step by Step guide to configure Eclipse and Apache Tomcat‘ before you proceed any further.

Step-1: Create Dynamic Web Application

Create a new Dynamic Web Project by clicking on FIle -> New -> Dynamic Web Project


Give a project name and click on Finish.


Step-2: Create a HTML page

Create a new html page by right clicking on WebContent and selecting New -> HTML File.


Put in a filename helloWorld.html and click on Finish.


Put in this code in helloWorld.html.

<!DOCTYPE html>
 <meta charset="ISO-8859-1">
 <title>Hello World</title>
 <h5>Hello World - html</h5>

Step-3: Create a JSP page

Create a new jsp page by right clicking on WEB-INF and selecting New -> JSP File.



Put in a filename helloWorld.jsp and click on Finish.



You are seeing any compilation errors then make sure that you have right Runtimes checked as shown below.


Put in below code into the jsp.

<%@ 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>Hello World</title>
<h5>Hello World - jsp</h5>

Step-4: Add Project to Server.

Add Project server to Server.



Step-5: Access new pages.

Access html page by url in browser. Url will be in “http://localhost:<port>/HelloWorld/helloWorld.html&#8221; format


Access jsp page by url in browser. Url will be in “http://localhost:<port>/HelloWorld/helloWorld.jsp&#8221; format




Step by Step guide to configure Eclipse and Apache Tomcat

This post has been moved to


Eclipse is a very commonly used IDE by developers across the globe. Eclipse with Apache Tomcat server is a great combination for beginners and for experienced web developers.

This post will provide step-by-step guidance to set up Eclipse with Tomcat Server.

Note: Below screen shots are for windows 64-bit version. Please act accordingly for 32-bit versions.

Step-1: Download Java.

Click Here to download Java 7 SDK.


You will see that there are two folder created in your C:\Program files\Java. (In my case I already had Java 6 installed too).


Step-2: Download Apache Tomcat Server

Click Here to download Apache Tomcat Server 7.


Once dowloaded, extract the server to D:\Software. You will see following structure after the extract is complete.


Note: you can also choose to install from 32-bit/64-bit Windows Service Installer if you want to install it like a service.

Step-3: Configure Environment Variables

Configure following environment variables and restart your pc when done.

  • JAVA_HOME: “C:\Program Files\Java\jdk1.7.0_79”java_home_environment_variable
  • JRE_HOME: “C:\Program Files\Java\jre7”jre_home_environment_variable
  • CATALINA_HOME: “D:\Software\apache-tomcat-7.0.67”catalina_home_environment_variable


Step-4: Download Eclipse

Click Here to download Eclipse. For this guide, I am downloading Eclipse Mars. But you can download anything that is compatible with Java version you downloaded.


Once downloaded extract contents to D:\Software.  You will see following structure after download is complete.



Step 5: Setup Server Configuration in Eclipse

Open Eclipse and Open Server View (Window -> Show View -> Servers)

Right Click New -> Server


Select Tomcat 7. You might have to Add Server runtime environment.


(Dialog when clicked on ‘Add…’ in above screen shot)


Click on Finish when Done.

Double click on Server to open deployment descriptor and make sure that you have default ports.


Now start the server by clicking on start button and you will see that server has started successfully.



Hope this Set up process was helpful. Please use the comment section if you face any issues setting up and I will help  you as soon as I can.



What IDE is preferable for ADF Development – Eclipse (or) Oracle JDeveloper?

This post has been moved to


Oracle’s ADF is an excellent JSF framework for web developers. When I decided to use ADF, I had a decision to make about the IDE that I had to use for development. As I have used Eclipse for quite sometime, my first choice was to use eclipse to realize later that it was a very big mistake. I would strongly recommend developers to use JDeveloper if you are using ADF for the following reasons:

  • Eclipse plugin for ADF is still in the beginning stages of developement and it has a long way to be able to emulate all the features in JDeveloper.
  • Eclipse Plugin doesn’t support JSF 2.0 by default. You will have to manually update the latest jars to get JSF 2.0 support. Even if you do this update IDE still doesn’t give your the features expected from IDE like code assist (Ctrl+Space). Eitherways I tried this but ran into other issues.
  • Eclipse Plugin doesn’t come with ADF runtime, you can again configure it on your own. With JDeveloper it comes out of the box.
  • Declarative programming for components like bread crumbs, bounded workflows are only present in JDeveloper.
  • Oracle’s documentation for advanced features is always talked interms of JDeveloper only.
  • ADF Forums are filled up with developers only using JDeveloper, so you will find very little help if you have questions relating to eclipse.

Hope this helps.

Error logs in Flash Builder or Flex Builder

This post has been moved to


At times you will have errors from Flash builder itself like memory running out or any internal error that might occur. I had one such error but the error description was not clear (it said some thing like internal error occurred during build), so I had to look for Flash builder error log. Surprisingly there is no much information on web about location of Flash Builder error log. After some research I found it and I am posting it with the hope that it will save time for online community.

Here are the steps:

1) In Flash builder click on Help -> ProductDetails. Then click on Installation Details.

2) Click on Configuration Tab and then click on Error log.

The error logs trace back to <WORKSPACE_LOCATION>\.metadata\.plugins\org.eclipse.ui.workbench\log