How To Create Java Rest Client To Retrieve Data

Till now we have seen how to write rest services to get JSON and XML  responses.  In this tutorial let us see how to write a simple Java class which call the rest service and retrieve data.

Here is the code to read XML response from Rest Service.

Things to Remember

  1. Rest Service should be running
  2. Url connection is established using java.net package classes.
  3. Once the connection is established then stream reader is used to read the contents from the web service.
  4. Content-Type is set as application/xml since we are calling a web service which returns xml data.

RestClient.java

package com.freetipscentral;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
 
public class RestClient {
	public static void main(String[] args) {
		try {
 
			URL url = new URL("http://localhost:8080/RestWebService/rest/xmlBooks/1");
			URLConnection connection = url.openConnection();
			connection.setRequestProperty("Content-Type", "application/xml");
 
			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			boolean hasData=true;
			while (hasData) {
				String data = in.readLine();
				if(data == null) {
					hasData=false;
				} else {
					System.out.println(data);
				}
			}
			System.out.println("\nREST Service Invoked Successfully and Data has been retrieved.");
			in.close();
		} catch (Exception e) {
			System.out.println("\nError while calling REST Service");
			System.out.println(e);
		}
	}
}

Here is the Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><book><name>Harry Potter</name><serialNumber>1</serialNumber></book>
 
REST Service Invoked Successfully and Data has been retrieved.

 

Here is the code to read JSON response from Rest Service.

Things to Remember

  1. Rest Service should be running
  2. Url connection is established using java.net package classes.
  3. Once the connection is established then stream reader is used to read the contents from the web service.
  4. Content-Type is set as application/json since we are calling a web service which returns json data.

RestClient.java

package com.freetipscentral;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
 
public class RestClient {
	public static void main(String[] args) {
		try {
 
			URL url = new URL("http://localhost:8080/RestWebService/rest/books/1");
			URLConnection connection = url.openConnection();
			connection.setRequestProperty("Content-Type", "application/json");
 
			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			boolean hasData=true;
			while (hasData) {
				String data = in.readLine();
				if(data == null) {
					hasData=false;
				} else {
					System.out.println(data);
				}
			}
			System.out.println("\nREST Service Invoked Successfully and Data has been retreived.");
			in.close();
		} catch (Exception e) {
			System.out.println("\nError while calling REST Service");
			System.out.println(e);
		}
	}
}

 

Here is the Output

{"name":"Harry Potter","serialNumber":1}
 
REST Service Invoked Successfully and Data has been retreived.