JAX-RS Tutorial

So far we have discussed the basics of Web services. In the just previous chapter we have seen an example in JAX-WS.This chapter would give a simple JAX-RS Tutorial.

Overview to JAX-RS

JAX-RS provides support to RESTful Web Services.REST stands for Representational State Transfer.JAX-RS 2.0 is the latest  JAX-RS specification

Features of RESTful Web Service

  • Neither XML Messaging  nor WSDL definition needed
  • Development of RESTful Web Service is easier  when compared with SOAP
  • Services are completely stateless
  • REST is well suited for PDAs and Mobile Phones . Using SOAP Web  Services in such low profile devices is obviously an overhead.
  • Since there is no formal web service interface definition , there should be a mutual understanding between the service and the client regarding the data being passed along.
  • RESTful Web Service exposes a set of resources .Clients can identify these resources using URIs.
  • Resources are manipulated using PUT,GET,DELETE and POST operations. A PUT operation creates a resource, POST transfers a new state to a resource, GET retrieves the current state of the resource , DELETE operation deletes a resource

JAX-RS Implementations

There are several implementations for JAX-RS. Some of them are listed below

Annotation support in JAX-RS

  1. @Path :-It specifies the relative  path for a class or method
  2. @GET : -Indicates that the  http request type of the resource is GET
  3. @PUT :- Indicates that the http request type of the resource is PUT
  4. @POST :- Indicates that the http request type of the resource is POST
  5. @DELETE : -Indicates that the http request type of the resource is DELETE
  6. @HEAD :- Indicates that the http request type of the resource is HEAD
  7. @Produces :- It specifies the MIME media type of the response of a resource
  8. @Consumes : -It specifies the MIME media type of the request to a resource
  9. @PathParam :-It is using to extract a parameter from the uri path of a resource
  10. @QueryParam :-It is using to extract query parameters from the uri path of a resource

Now let us see a sample RESTful web service.

Software Tools Needed

Eclipse Indigo :- Download Link

Apache Tomcat 7 :- Download Link

Java  EE SDK 1.6 :- Download Link

Mozilla Firefox :- Download Link

Steps

1)Extract eclipse and Tomcat to suitable directories.Open eclipse in a suitable workspace.

2)Now create a dynamic web project in eclipse . Give a suitable name.In this case let it be  RESTSample.

13)Press Next Button . In the next window also press Next.Then in the coming window select the check box.Then press Finish button

2

4)Now right click  in the server  console and create  a new Tomcat instance.

3

5)Locate the server location and add the newly created project to the server.And then press Finish

46)Now double click on the server instance in the console  and then select the highlighted radio button.And then save the server details by pressing CTRL + S

5

7) Now download  RESTEasy  API from sourceforge. In this example we are using version 3.0 Final. Download it and extract it to suitable folder. Take the jar files from the lib and embedded-lib  directories . Then put those jar files in WebContent/WEB-INF/lib directory.

8)Now create a package in src.Give a suitable name. In this case it is com.rest.services

9)Now create a Java file in the newly created package.To be simple , we can discuss a GET service which returns a String message when an http request hits.

10)Now edit the web.xml  file in the WebContent/WEB-INF  directory.

10)Now build and publish the application to server.Then start the server.If  an error ‘No class def found error : javax/enterprise/context/spi/Contextual’ then remove the resteasy-cdi jar from the lib .Then rebuild and republish.

11)So the server is started.Now we need to test the locally deployed rest service.For that we can use REStClient plugin of Mozilla Firefox.Download it from Mozilla update site.

12)Open the RESTClient in firefox. Select the method name as GET . Type the url as :http://localhost:8080/RESTSample/sampleService/getService

13)Now Press the send button.

6

14)If we click on the Response Body tab in the RESTClient we can see the response string message.

7

    See Related Discussions

WebService Overview

JAX-WS Tutorial

4 thoughts on “JAX-RS Tutorial

  1. I have followed every step as described below but still getting 404 error,actully i have tried with jersey too but i am getting the same error,,please help me stucked in this problem from 3 days.

  2. Even i have followed each and every step,still even i am getting 404….please do help me!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">