Client library for REST web services in Java


I'm in a project where you need to access a REST web service and would like to know which Java library is most commonly used to access this type of resource in an easy way.

I know that it's even possible to do everything in the hand using the Java standard classes, but I'm looking for something that simplifies the work a little, and also knows the market standard for it.

asked by anonymous 24.01.2014 / 14:57

4 answers


Another good option is JBoss RESTEasy . In addition to implementing the JAX-RS 2.0 Client standard (ie it is possible to make calls with the same code as @utluiz demonstrated for Jersey), the library has its own proxies-based API.

Sample documentation :

Client client = ClientFactory.newClient();
WebTarget target ="");
ResteasyWebTarget rtarget = (ResteasyWebTarget)target;

SimpleClient simple = rtarget.proxy(SimpleClient.class);
client.putBasic("hello world");

Where SimpleClient is an annotated interface (can be even the same as the server):

public interface SimpleClient {
   void putBasic(String body);

   // ... Demais métodos

Some other options besides the Jersey and RESTEasy

Finally, while this is not necessarily practical, you can always use raw HTTP APIs to consume REST services. HttpURLConnection , Apache HttpClient , etc. This gives you more control in exchange for much more extensive code (and susceptible to errors).

For a design Open Source / article that I am writing together with a colleague, we have decided to code a method that upload images to Facebook on the POST with multipart / form-data encode) and compare with the code of a specialized library RestFB ). You can see the difference in size and complexity of the publicarRestFB vs publicarGraphAPI to feel the drama.

24.01.2014 / 16:15

I can not say it is the most widely used framework (this is debatable), but I use the Jersey , implementation API reference JAX-RS. It implements both the server part and the client part.

See an example of using the Client API :

Client client = ClientBuilder.newClient();
WebTarget target ="http://localhost:9998").path("resource");

Form form = new Form();
form.param("x", "foo");
form.param("y", "bar");

MyJAXBBean bean =
24.01.2014 / 15:20

I use the org.apache.httpcomponents library

If you use maven in your project, here are the dependencies

<!-- Httpclient: -->
        <!-- Common-HttpClient: -->

Usage example

public String doRequestPost(String aRota) throws HttpException, IOException {
            HttpClient httpClient = new HttpClient();
            PostMethod httpPost = new PostMethod("http://%s:%s/"+aRota);
            httpPost.addParameter("nome", "valor");
            return httpPost.getResponseBodyAsString();
24.01.2014 / 19:16

In this link there is a step-by-step 'Hello World' using Jersey in the Restful WebService implementation in Java.

24.01.2014 / 15:42