juneau-rest-client
Maven Dependency

<dependency> <groupId>org.apache.juneau</groupId> <artifactId>juneau-rest-client</artifactId> <version>8.2.0</version> </dependency>

Java Library

juneau-rest-client-8.2.0.jar

OSGi Module

org.apache.juneau.rest.client_8.2.0.jar

Built upon the feature-rich Apache HttpClient library, the Juneau RestClient API adds support for fluent-style REST calls and the ability to perform marshalling of POJOs to and from HTTP parts.

Example:

// Create a basic REST client with JSON support and download a bean. MyBean bean = RestClient.create() .simpleJson() .build() .get(URI) .run() .assertStatus().code().is(200) .assertHeader("Content-Type").matchesSimple("application/json*") .getBody().as(MyBean.class);

Breaking apart the fluent call, we can see the classes being used:

RestClientBuilder builder = RestClient.create().simpleJson(); RestClient client = builder.build(); RestRequest req = client.get(URI); RestResponse res = req.run(); RestResponseStatusLineAssertion statusLineAssertion = res.assertStatus(); FluentIntegerAssertion<RestResponse> codeAssertion = statusLineAssertion.code(); res = codeAssertion.is(200); FluentStringAssertion<RestResponse> headerAssertion = res.assertHeader("Content-Type"); res = headerAssertion.matchesSimple("application/json*"); RestResponseBody body = res.getBody(); MyBean bean = body.as(MyBean.class);

It additionally provides support for creating remote proxy interfaces using REST as the transport medium.

Example:

// Define a Remote proxy for interacting with a REST interface. @Remote(path="/petstore") public interface PetStore { @RemoteMethod(method=POST, path="/pets") Pet addPet( @Body CreatePet pet, @Header("E-Tag") UUID etag, @Query("debug") boolean debug ); } // Use a RestClient with default Simple JSON support. RestClient client = RestClient.create().simpleJson().build(); PetStore store = client.getRemote(PetStore.class, "http://localhost:10000"); CreatePet createPet = new CreatePet("Fluffy", 9.99); Pet pet = store.addPet(createPet, UUID.randomUUID(), true);