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 need not 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
- @Path :-It specifies the relative path for a class or method
- @GET : -Indicates that the http request type of the resource is GET
- @PUT :- Indicates that the http request type of the resource is PUT
- @POST :- Indicates that the http request type of the resource is POST
- @DELETE : -Indicates that the http request type of the resource is DELETE
- @HEAD :- Indicates that the http request type of the resource is HEAD
- @Produces :- It specifies the MIME media type of the response of a resource
- @Consumes : -It specifies the MIME media type of the request to a resource
- @PathParam :-It is using to extract a parameter from the uri path of a resource
- @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.
3)Press Next Button . In the next window also press Next.Then in the coming window select the check box.Then press Finish button
4)Now right click in the server console and create a new Tomcat instance.
5)Locate the server location and add the newly created project to the server.And then press Finish
6)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
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.
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("sampleService") public class SampleService { public SampleService() { } @GET @Path("getService") @Produces("application/txt") public String getService() { String message = "Hello ! This is a message from REST service"; return message; } }
10)Now edit the web.xml file in the WebContent/WEB-INF directory.
RESTSample index.html rest-servlet org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher 1 rest-servlet /* resteasy.scan true resteasy.servlet.mapping.prefix / org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
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.
14)If we click on the Response Body tab in the RESTClient we can see the response string message.
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.
Please double check the servlet mapping part
Even i have followed each and every step,still even i am getting 404….please do help me!
Hi!!… did you get the solution?