Friday, November 2, 2012

REST Server and Client in Java

This simple example shows how we can have a server side REST module and client sending REST requests to the server

The server side Eclipse project looks like this with the following JAR's added to the project. We also need to add these JAR's to the client side application shown below

The Server side REST application "Hello.java" looks like this
package com.giri.rest;

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

@Path("/hello")
public class Hello {
 // this method is called if TEXT_PLAIN is the request
 @GET
 @Produces(MediaType.TEXT_PLAIN)
 public String plainTextMethod() {
  return "Hello, I'm a plain text message !";
 }
 
 // This method is called if XML is the request
 @GET
 @Produces(MediaType.TEXT_XML)
 public String xmlMethod() {
  return "" + " Hello XML" + "";
 }
 
 // this method is called if HTML is the request
 @GET
 @Produces(MediaType.TEXT_HTML)
 public String htmlMethod() {
  return "--[ insert HTML code you would like to insert here ]--";
 }
 
}

Client application which send the REST request to the server "ClientTestMain.java" looks like this
package com.giri.rest;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

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 ClientTestMain {

 private static URI getBaseURI() {
  return UriBuilder.fromUri("http://localhost:8080/RestExample/").build();
 }
 
 public static void main(String[] args) {
 
  
  ClientConfig clientConfig = new DefaultClientConfig();
  Client client = Client.create(clientConfig);
  
  WebResource service = client.resource(getBaseURI());
  
  System.out.println("Fluent Interfaces response:");
  System.out.println(service.path("giri").path("hello").accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());
  System.out.println("\n");
  
  System.out.println("TEXT_PLAIN response:");
  System.out.println(service.path("giri").path("hello").accept(MediaType.TEXT_PLAIN).get(String.class));
  System.out.println("\n");
  
  System.out.println("TEXT_XML response:");
  System.out.println(service.path("giri").path("hello").accept(MediaType.TEXT_XML).get(String.class));
  System.out.println("\n");
  
  System.out.println("TEXT_HTML response:");
  System.out.println(service.path("giri").path("hello").accept(MediaType.TEXT_HTML).get(String.class));
  System.out.println("\n");

 }
}

The "web.xml" file of the DYNAMIC WEB PROJECT looks like this:



Output after running the client side application
Fluent Interfaces response:
GET http://localhost:8080/RestExample/giri/hello returned a response status of 200 OK


TEXT_PLAIN response:
Hello, I'm a plain text message !


TEXT_XML response:
 Hello XML


TEXT_HTML response:
--[ here goes the generated HTML response - I have removed HTML code because it has messed up my page here :D  ]--

No comments :

Post a Comment