Leaderboard1

Leaderboard2

Wednesday, May 21, 2014

RESTful Web Service with Java: Tutorial

RESTful Web Services

This post will demonstrate how to create a RESTful Web Service and client using Jersey framework which extends JAX-RS API.

REST fundamentals
  • Everything in REST is considered as a resource.
  • Every resource is identified by an URI.
  • Uses uniform interfaces. Resources are handled using POST, GET, PUT, DELETE operations which are similar to Create, Read, update and Delete(CRUD) operations.
  • Be stateless. Every request is an independent request. Each request from client to server must contain all the information necessary to understand the request.
  • Communications are done via representations. E.g. XML, JSON.

Download Jersey zip bundle from here.

You will need these jars:







  • asm-3.1.jar
  • jersey-client-1.17.1.jar
  • jersey-core-1.17.1.jar
  • jersey-server-1.17.1.jar
  • jersey-servlet-1.17.1.jar
  • jsr311-api-1.1.1.jar

  • Now create a new class UserInfo.java.



    package com.restws;

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("UserInfoService")
    public class UserInfo {

     @GET
     @Path("/name/{i}")
     @Produces(MediaType.TEXT_XML)
     public String userName(@PathParam("i") String i){
      
      String name = i;
      return "" + "" + name + "" + "";
      
     }

     @GET
     @Path("/age/{j}")
     @Produces(MediaType.TEXT_XML)
     public String userAge(@PathParam("j") int j){
      
      int age = j;
      return "" + "" + age + "" + "";
      
     }

    Now create web.xml file in WEb-INF folder.

    }
      To run the project, right click on it and click on run as ->run on server.
    http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Akash

    Now create MyClient.java:

    package com.restclient;

    import javax.ws.rs.core.MediaType;
    import com.sun.jersey.api.client.Client;
    import com.sun.jersey.api.client.ClientResponse;
    import com.sun.jersey.api.client.WebResource;
    import com.sun.jersey.api.client.config.ClientConfig;
    import com.sun.jersey.api.client.config.DefaultClientConfig;

    public class MyClient {

      public static final String BaseURI = "http://localhost:8080/RESTfulWS";
     public static final String PATH_NAME = "/MyService/name/";
     public static final String PATH_AGE = "/MyService/age/";

      public static void main(String... args) {

       String name = "Akash";
      int age = 26;

       ClientConfig config = new DefaultClientConfig();
      Client client = Client.create(config);
      WebResource resource = client.resource(BaseURI);

       WebResource nameResource = resource.path("rest").path(PATH_NAME + name);
      System.out.println("Client Response \n"
        + getClientResponse(nameResource));
      System.out.println("Response \n" + getResponse(nameResource) + "\n\n");

       WebResource ageResource = resource.path("rest").path(PATH_AGE + age);
      System.out.println("Client Response \n"
        + getClientResponse(ageResource));
      System.out.println("Response \n" + getResponse(ageResource));
     }

      /* Returns client response */
     private static String getClientResponse(WebResource resource) {
      return resource.accept(MediaType.TEXT_XML).get(ClientResponse.class)
        .toString();
     }

      /* Returns the response as XML */
     private static String getResponse(WebResource resource) {
      return resource.accept(MediaType.TEXT_XML).get(String.class);
     }
    }


    Run MyClient.java as Java applicaiton, you will get following output:

    Client Response
    GET http://localhost:8080/RESTfulWS/rest/MyService/name/Akash returned a response status of 200 OK
    Response
    Akash

    Client Response
    GET http://localhost:8080/RESTfulWS/rest/MyService/age/26 returned a response status of 200 OK
    Response
    26

    No comments:

    Post a Comment