Rest Web Service

How to create client for restful web service

Create a student class(Hello.java) as shown below
package com.vogella.jersey.first;

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("/hello")
public class Hello {

  // This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey My first rest service";
  }
/*
  // This method is called if XML is request
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  }
*/
   @GET
   @Path("{no1}/{no2}")
   @Produces(MediaType.TEXT_PLAIN)   
   public String getStudentsInfo(@PathParam("no1") String number1,@PathParam("no2") String number2  ){
      
       return "Sum of the number you send as ==>" + (Integer.parseInt(number1) + Integer.parseInt(number2));
   }
  
   @GET
   @Path("{no1}")
   @Produces("application/xml")   
   public String getStudentsInfo1(@PathParam("no1") String number1){
       return "<Root><data>"+number1+"</data></Root>";
   }
  
  
  @GET
  @Path("/userList")
  @Produces("application/xml")
  public String getUserList()
  {
      User user =new User();
      String userlist=user.getUserList();
      return userlist;
  }
}

And now create on simple User.java class as shown below

package com.vogella.jersey.first;

public class User {
    public String getUserList() {
         return "<Users><user1>shivkumar</user1><user2>shivsharma</user2>";
    }
}


In above class(hello.java) i have shown some methods which will provide response based on input URL. once you create above two classes you can run in simple tomcat server just like simple application.
 
Now let try to write very easy client for this

package com.jersey.Client;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class RestClient {
   
    public static void main(String []args){
        RestClient client = new RestClient();
        client.SayHello();
    }
   
 private void SayHello(){
       
        Client client = Client.create();
        WebResource  webResource =client.resource("http://localhost:8081/MyResTProjects/rest/hello/userList");
        ClientResponse response =webResource.accept("application/xml").get(ClientResponse.class);
       
        String output = response.getEntity(String.class); 
        System.out.println(output);
    }
}


Now you can run above application as simple java application

and you will get response like
<Users><user1>shivkumar</user1><user2>shivsharma</user2>


You can try some different method to run.


No comments:

Post a Comment