Java client library for REST web services

Asked

Viewed 3,728 times

15

I am in a project in which it is necessary to access a web service REST and I would like to know which is the most used Java library to access this type of resource in an easy way.

I know it is even possible to do everything by hand using the standard Java classes, but I am looking for something that will simplify the work a little, and also know what is the market standard for this.

4 answers

11


Another good option is the Resteasy of Jboss. In addition to implementing the JAX-RS 2.0 Client standard (i.e., it is possible to make calls with the same code @utluiz demonstrated for Jersey), the library has its own API based on proxies.

Example of 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 error-prone).

To a little project Open Source / article I’m writing along with a colleague, we decided to encode a method that makes upload of images for Facebook in the nail (POST with Encounter Multipart/form-date) and compare with the code of a specialized library (Restfb). You can see the difference in size and complexity of methods publicarRestFB vs publicarGraphAPI to feel the drama.

7

I can’t say if it’s the most used framework (this is very debatable), but I use the Jersey, to reference implementation of the JAX-RS API. It implements both the server part and the client part.

See an example of using 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);
  • Just linking to client documentation and remembering that for the client only the jersey-client and optionally a Connector (guide for the Maven). In innocence, in a situation a few times ago, we did a lambança in the Classpath trying to set up the client, whereas the App Server already makes available all that was needed through a Shared library (hidden but existing) :D.

1

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

  • Just one note: the question refers to a library client but indicated tutorial talks about how to create the server service.

  • 5

    The relevant content that can be found on link shall be placed in the body of the response. Link-only responses tend to be closed because if the link is no longer available the answer is no longer valid or contribute something useful.

1

I use the library org.apache.httpcomponents

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>

Example of use

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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.