Creating Hello World Service using Jersey

   This blog entry shows how easy it is to build a Hello World RESTful Web Service using Jersey

Required Setup:

  1. Download latest version of NetBeans, if you do not already have it. NetBeans provides wizards for creating RESTful Web Service using Jersey.
  2. If you do not have GlassFish installed in your machine, install one which comes with NetBeans setup. We will use GlassFish for deployments (but, it can be deployed on other containers too).

We will create the simple HelloWorldService in the following steps:

  1. In the NetBeans IDE, create a new project by selecting the "Java Web > Web Application" wizard.
  2. Name the project as HelloWorldWebapp.
  3. Set the server to GlassFish v2 as shown in the snapshot, and say Finish:  Select GlassFish v2 from the servers
  4. Right click on the project in the left pane, and select "New" > "Other". Select New > Other from the menu
  5. In the "Choose File Type" dialog, select "Web Services" and "RESTful Web Services from Patterns". Select Web Serv ices > RESTful Web Service from Patterns
  6. Set the pattern to Singleton in the next screen.
  7. In the next screen, set the package name to mycompany.resources. Set path to hello, class name to HelloResource and MIME-type to text/plain. Click the Finish button.
  8. The wizard creates the HelloResource class which has methods annotated with @GET and @PUT.
  9. Modify the @GET annotated method name to sayHello() and add the statement return "Hello World! Jersey welcome you";
  10. Have a look at the generated web.xml: it has a mapping of the Jersey SPI defined servlet com.sun.jersey.spi.container.servlet.ServletContainer to the url-patterns /resources/\* .
  11. Deploy the application.
  12. In a browser, enter the URL http://localhost:8080/HelloWorldWebapp/resources/hello. It gives a response with the string Hello World! Jersey welcome you.
  13. These are the steps for creating a resource using NetBeans and Jersey. 

Overview of the generated resource class

           It can be seen that the generated class has some annotations over the method implementations and the class definition. Lets see what these annotations are and what they mean:

  • @Path("hello") : This annotation describes the path to the resource, i.e., any request with the path "hello" would be routed to this resource.
  • @GET: This annotation maps the method sayHello to the HTTP GET request sent to the HelloResource.
  • @Produces("text/plain"): This annotation describes the mime type of data, which can be produced by the resource. If a resource supports multiple representations of data, all the supported mime-types could be declared as an array of strings in the Produces annotation. For ex: @Produces({"text/plain", "application/xml"}) means that the resource is capable of serving requests which have the Accept header set to either text/plain or application/xml.
  • @Consumes("text/plain"): This annotation describes the mime type of data which a resource can consume. Similar to the Produces annotation if the resource is capable of consuming multiple representations of data, all these representations could be declared as an array of strings in the Consumes annotation.
  • @PUT: This annotation maps the annotated method to the HTTP PUT request sent to the resource.

In the next entries we will see how to use the other features provided by JAX-RS/Jersey to access Uri Parameters, Query Parameters, Client API, etc. Also we will see how to deploy the applications to light weight HTTPServer, GrizzlyWebContainer and EmbeddedGlassFish.

Comments:

test

Posted by guest on February 01, 2011 at 08:40 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Naresh worked at Sun Microsystems for two years. During these two years he had worked on the Project Metro and Project Jersey.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today