RESTFul Web Service Client Example

In the previous section  we have seen  a sample web service . In this section we can write a simple client for the service we developed

Restful Web Service Client example

RESTFul Service Client for a GET Service

Let us start with a client for a GET service.Once completion we can develop the same for a POST,PUT and DELETE  services

Before starting the  coding part of client , publish the web service we already developed  and start the server.

There are many libraries available for us to develop RESTful service clients. We can use the Apache Http Client libraries or the  JAX-RS implementation specific libraries.In our example we are using Apache Http Client components.We can download it from the Apache web site.We are using version 4.3 in our example. Extract and put the jar files in the lib folder to our project path.

The jars are :

commons-codec-1.6

commons-logging-1.1.3

fluent-hc-4.3.4

httpclient-4.3.4

httpclient-cache-4.3.4

httpcore-4.3.2

httpmime-4.3.4

RestClientSample.java

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class RestClientSample {

	public static void main(String[] args) {
		RestClientSample sample = new RestClientSample();
		sample.getResponse();
	}

	public void getResponse() {

		HttpClient client = HttpClientBuilder.create().build();
		HttpGet getRequest = new HttpGet(
				"http://localhost:8080/RESTSample/sampleService/getService");
		try {
			HttpResponse response = client.execute(getRequest);
			HttpEntity entity = response.getEntity();
			String responseString = EntityUtils.toString(entity, "UTF-8");
			System.out.println(responseString);

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

Now run the code , and the output would be the String message

RESTFul Service Client for a POST Service

Now add a simple POST  service also to the SampleService.java we already developed.So the SampleService.java would be

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
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;
	}

	@POST
	@Path("postService")
	@Produces("text/plain")
	@Consumes("text/plain")
	public String postService(String message) {
		System.out.println("Message entered is  " + message);
		String reply = "The string you entered is :" + message;
		return reply;
	}

}

Compile and republish it to server.Now we need to develop a client for the new service also .Modify the Client class as shown below and run it.

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class RestClientSample {

	public static void main(String[] args) {
		RestClientSample sample = new RestClientSample();
		sample.getResponse();
		sample.getPostResponse();
	}

	public void getResponse() {

		HttpClient client = HttpClientBuilder.create().build();
		HttpGet getRequest = new HttpGet(
				"http://localhost:8080/RESTSample/sampleService/getService");
		try {
			HttpResponse response = client.execute(getRequest);
			HttpEntity entity = response.getEntity();
			String responseString = EntityUtils.toString(entity, "UTF-8");
			System.out.println(responseString);

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void getPostResponse() {
		HttpClient client = HttpClientBuilder.create().build();
		HttpPost postRequest = new HttpPost(
				"http://localhost:8080/RESTSample/sampleService/postService");

		try {
			postRequest.setEntity(new StringEntity("Hello.!"));
			HttpResponse response = client.execute(postRequest);
			HttpEntity entity = response.getEntity();
			String responseString = EntityUtils.toString(entity, "UTF-8");
			System.out.println(responseString);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

}

It would display the output of both the services.

Related Discussions

Web Services – An Over view

JAX-WS Tutorial

JAX-RS Tutorial

2 thoughts on “RESTFul Web Service Client Example

Leave a Reply

Your email address will not be published. Required fields are marked *