• Technical
  • How to consume REST Web Service in Java
Header

How to consume REST Web Service in Java

Hi folks,

Hope you guys will be doing great. Today I am going to show you how to consume REST Web Service in Java.

So, first thing first. What is Web Service?

Web Services is a technology which provides an efficient way to share application logic across multiple machines running various operating systems and using different development environments.

If you guys want to know more about Web Services, then Google up ;-). But one thing, don’t jump into advance stuff. Clear you basics first or things could get messy 😛

Moving ahead, web services are of two types:

1. SOAP
2. REST

In this post I will cover only REST web service. So, what is REST Web Service?

Representational State Transfer (REST) is an architectural style that abstracts the architectural elements within a distributed hypermedia system. REST ignores the details of component implementation and protocol syntax in order to focus on the roles of components, the constraints upon their interaction with other components, and their interpretation of significant data elements.

In 2000, Roy Fielding wrote his doctoral dissertation about architectural styles and designs of network-based software architectures. Within it, he speaks of Web architectures where any information or concept that can be named, referred to as a resource, is identified by a resource identifier, such as a URI. In common terms, you can think of the Yahoo! home page as a resource with a resource identifier of http://www.yahoo.com/.rest-featured

Components in this architecture perform actions on the resource using its representation. For instance, using a browser to request the resource, a server transfers the current state of a Web page to the browser. The browser is then able to perform the action of rendering the representation. Simply put, the server sends the data for the requested page in its current state, and the data is rendered. Navigating to any of the links within the rendered page causes a state transition because the next page, which could be considered another state of an application, is transferred to the browser for rendering.

For more details on REST Web Service, please check the article by Roy Fielding – http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

Let’s check out the code now:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class RestWS {

	public void callRestWS() {

		String response = null;
		try {

			URL url = new URL("http://www.someurl.com"); // put url
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("GET"); // HTTP Methods

			conn.setRequestProperty("key", "value");
			conn.setRequestProperty("Content-Type", "text/xml"); // set if server is expecting content type

			OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());

			out.write("<msg>Rest Client</msg>");  // set inputs like JSON string or XML string that you need to send as request

			out.close();

			conn.disconnect();
			conn.setDoInput(true);

			InputStream is = conn.getInputStream();
			Scanner isScanner = new Scanner(is);

			StringBuffer buf = new StringBuffer();
			while(isScanner.hasNextLine()) { 
				buf.append(isScanner.nextLine() +"\n");
			}
			response = buf.toString();
			System.out.println(response);

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

	public static void main(String[] args) {
		new RestWS().callRestWS();
	}
}

Easy, isn’t it?

rest-featuredThis is the most basic implementation. The reason to choose this example is to tell that REST web service work on HTTP and uses HTTP methods, PUT, GET, POST, DELETE, HEAD, OPTIONS, TRACE and CONNECT. So, it is like just accessing some URL through your code (using different HTTP methods). There are several frameworks/tools available which help you to consume REST web services. You can try those too. It will be fun.

In my next post, I will publish same thing but the technology will be different. 😉

 

That’s all folk.

Critics/suggestion are very much welcome.

Have a nice day ahead.

6,796 total views, 1 views today

This entry was posted in Technical

2 Responses



Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.

Follow Me