This means that the code on the client side can be changed at any time without affecting the operation of the server, and the code on the server side can be changed without affecting the operation of the client.
A REST API works essentially the same way that any website does. A call is made from a client to a server, and data is received back over the HTTP protocol. Facebook's Graph API is an easy way to show the similarities between a REST API call and the loading of a webpage.
Update: It’s been almost 5 years since I wrote the answer below; today I have a different perspective.
99% of the time when people use the term REST, they really mean HTTP; they could care less about “resources”, “representations”, “state transfers”, “uniform interfaces”, “hypermedia”, or any other constraints or aspects of the REST architecture style identified by Fielding. The abstractions provided by various REST frameworks are therefore confusing and unhelpful.
So: you want to send HTTP requests using Java in 2015. You want an API that is clear, expressive, intuitive, idiomatic, simple. What to use? I no longer use Java, but for the past few years the Java HTTP client library that has seemed the most promising and interesting is OkHttp. Check it out.
You can definitely interact with RESTful web services by using URLConnection
or HTTPClient to code HTTP requests.
However, it's generally more desirable to use a library or framework which provides a simpler and more semantic API specifically designed for this purpose. This makes the code easier to write, read, and debug, and reduces duplication of effort. These frameworks generally implement some great features which aren't necessarily present or easy to use in lower-level libraries, such as content negotiation, caching, and authentication.
Some of the most mature options are Jersey, RESTEasy, and Restlet.
I'm most familiar with Restlet, and Jersey, let's look at how we'd make a POST
request with both APIs.
Form form = new Form();
form.add("x", "foo");
form.add("y", "bar");
Client client = ClientBuilder.newClient();
WebTarget resource = client.target("http://localhost:8080/someresource");
Builder request = resource.request();
request.accept(MediaType.APPLICATION_JSON);
Response response = request.get();
if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
System.out.println("Success! " + response.getStatus());
System.out.println(response.getEntity());
} else {
System.out.println("ERROR! " + response.getStatus());
System.out.println(response.getEntity());
}
Form form = new Form();
form.add("x", "foo");
form.add("y", "bar");
ClientResource resource = new ClientResource("http://localhost:8080/someresource");
Response response = resource.post(form.getWebRepresentation());
if (response.getStatus().isSuccess()) {
System.out.println("Success! " + response.getStatus());
System.out.println(response.getEntity().getText());
} else {
System.out.println("ERROR! " + response.getStatus());
System.out.println(response.getEntity().getText());
}
Of course, GET requests are even simpler, and you can also specify things like entity tags and Accept
headers, but hopefully these examples are usefully non-trivial but not too complex.
As you can see, Restlet and Jersey have similar client APIs. I believe they were developed around the same time, and therefore influenced each other.
I find the Restlet API to be a little more semantic, and therefore a little clearer, but YMMV.
As I said, I'm most familiar with Restlet, I've used it in many apps for years, and I'm very happy with it. It's a very mature, robust, simple, effective, active, and well-supported framework. I can't speak to Jersey or RESTEasy, but my impression is that they're both also solid choices.
If you are calling a RESTful service from a Service Provider (e.g Facebook, Twitter), you can do it with any flavour of your choice:
If you don't want to use external libraries, you can use java.net.HttpURLConnection
or javax.net.ssl.HttpsURLConnection
(for SSL), but that is call encapsulated in a Factory type pattern in java.net.URLConnection
.
To receive the result, you will have to connection.getInputStream()
which returns you an InputStream
. You will then have to convert your input stream to string and parse the string into it's representative object (e.g. XML, JSON, etc).
Alternatively, Apache HttpClient (version 4 is the latest). It's more stable and robust than java's default URLConnection
and it supports most (if not all) HTTP protocol (as well as it can be set to Strict mode). Your response will still be in InputStream
and you can use it as mentioned above.
Documentation on HttpClient: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html
This is very complicated in java, which is why I would suggest using Spring's RestTemplate
abstraction:
String result =
restTemplate.getForObject(
"http://example.com/hotels/{hotel}/bookings/{booking}",
String.class,"42", "21"
);
Reference:
RestTemplate
If you just need to make a simple call to a REST service from java you use something along these line
/*
* Stolen from http://xml.nig.ac.jp/tutorial/rest/index.html
* and http://www.dr-chuck.com/csev-blog/2007/09/calling-rest-web-services-from-java/
*/
import java.io.*;
import java.net.*;
public class Rest {
public static void main(String[] args) throws IOException {
URL url = new URL(INSERT_HERE_YOUR_URL);
String query = INSERT_HERE_YOUR_URL_PARAMETERS;
//make connection
URLConnection urlc = url.openConnection();
//use post mode
urlc.setDoOutput(true);
urlc.setAllowUserInteraction(false);
//send query
PrintStream ps = new PrintStream(urlc.getOutputStream());
ps.print(query);
ps.close();
//get result
BufferedReader br = new BufferedReader(new InputStreamReader(urlc
.getInputStream()));
String l = null;
while ((l=br.readLine())!=null) {
System.out.println(l);
}
br.close();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With