Allows calling JAX-RS REST interfaces with methods returning
CompletableFuture
or Orbit
Task
.
This enables writing REST client code in a fluent asynchronous way.
The project orbit-rest-client depends only on the JAX-RS standard and it is in principle compatible with any client library that provides javax.ws.rs.client.WebTarget. It has been tested with jersey-client.
public interface Hello
{
@GET
@Path("/")
Task<String> getHome();
}
public static void main(String args[])
{
WebTarget webTarget = getWebTarget("http://example.com");
Hello hello = new RestClient(webTarget).get(Hello.class);
Task<String> response = hello.getHome();
response.thenAccept(x -> System.out.println(x));
response.join();
}
// use any jax-rs client library to get javax.ws.rs.client.WebTarget
private static WebTarget getWebTarget(String host)
{
ClientConfig clientConfig = new ClientConfig();
Client client = ClientBuilder.newClient(clientConfig);
return client.target(host);
}
public interface Hello
{
@GET
@Path("/")
CompletableFuture<String> getHome();
}
public static void main(String args[])
{
WebTarget webTarget = getWebTarget("http://example.com");
Hello hello = new RestClient(webTarget).get(Hello.class);
CompletableFuture<String> response = hello.getHome();
response.thenAccept(x -> System.out.println(x));
response.join();
}
// use any jax-rs client library to get javax.ws.rs.client.WebTarget
private static WebTarget getWebTarget(String host)
{
ClientConfig clientConfig = new ClientConfig();
Client client = ClientBuilder.newClient(clientConfig);
return client.target(host);
}