JAX-RS Tutorial-Using Eclipse ,Tomcat (original) (raw)

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

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

  1. 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.

6

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

7

WebService Overview

JAX-WS Tutorial