JAX-RS - Standard Client API

[Updated: Apr 20, 2017, Created: Jan 26, 2016]

JAX-RS 2.0 introduced a standard JAX-RS client API. Now we don't have to use a third part API anymore. The API is based on SPI pattern so actual work is done by underlying implementation but our code is only dependent on JAX-RS API. In these tutorials, we will be using Jersey implementation.


Here's a quick code to have an idea how JAX-RS client API works

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://www.example.com/service/orders");
Response response = target.request().get();
System.out.println(response.getEntity());
response.close();
client.close();

Note, in previous examples, we have been using the same client API in our junit tests. In JUnit tests the Client is created in super class JerseyTest


Example Project

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run

After running server, we can run the JAX-RS client in separate JVM, by running main method of OrderServiceClient

Dependencies and Technologies Used :

  • jersey-core-server 2.22.1: Jersey core server implementation.
  • jersey-container-servlet 2.22.1: Jersey core Servlet 3.x implementation.
  • jersey-test-framework-provider-jdk-http 2.22.1: Jersey Test Framework - JDK HTTP container.
  • JDK 1.8
  • Maven 3.0.4

Jax Rs Client Api Example Select All Download
  • jaxrs-client-api-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • client
                • service

See Also