Client library for REST web services in Java

15

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

11

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 = client.target("http://example.com/base/uri");
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 {
   @PUT
   @Path("basic")
   @Consumes("text/plain")
   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
7

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 = client.target("http://localhost:9998").path("resource");

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

MyJAXBBean bean =
target.request(MediaType.APPLICATION_JSON_TYPE)
    .post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
        MyJAXBBean.class);
    
24.01.2014 / 15:20
1

I use the org.apache.httpcomponents library

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

<!-- Httpclient: http://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.3 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3</version>
        </dependency>
        <!-- Common-HttpClient: http://mvnrepository.com/artifact/commons-httpclient/commons-httpclient/3.1 -->
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>

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");
            httpclient.executeMethod(httpPost);
            return httpPost.getResponseBodyAsString();
}
    
24.01.2014 / 19:16
1

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