Contents

Overview

MicroProfile Rest Client adds the capability to invoke remote services by defining a Java interface with Jakarta REST (JAX-RS) annotations that resembles a server-side resource class.

Helidon will automatically create a proxy class for the interface and map local proxy calls to remote REST calls.

For more information, see Rest Client For MicroProfile Specification.

Maven Coordinates

To enable MicroProfile Rest Client either add a dependency on the helidon-microprofile bundle or add the following dependency to your project’s pom.xml (see Managing Dependencies).

<dependency>
    <groupId>io.helidon.microprofile.rest-client</groupId>
    <artifactId>helidon-microprofile-rest-client</artifactId>
</dependency>
Copied

API

ClassDescription
org.eclipse.microprofile.rest.client.RestClientBuilderBase builder instance. Contains configuration options and a build method that creates the actual client instance.
AnnotationDescription
@RegisterRestClientA marker annotation to register a client at runtime. This marker must be applied to any CDI managed clients.
@RestClientRestClient qualifier which should be used on an CDI injection points.

Creating a New Client Using a Builder

MicroProfile Rest Client can be created using a builder obtained from RestClientBuilder.newBuilder().

The builder provides methods to specify the client interface to be proxied as well as to configure additional details such as server URI, SSL context, connection timeouts, etc. Any method call on the resulting proxy object will be automatically translated into a remote call to the service using the provided configuration.

Example
SomeResource greetResource = RestClientBuilder.newBuilder()
                                 .baseUri(URI.create("http://localhost:8080/greet"))
                                 .build(GreetRestClient.class);
greetResource.getDefaultMessage();
Copied

The RestClientBuilder interface extends the Configurable interface from Jakarta REST (JAX-RS), enabling direct registration of providers such as filters, param converters, exception mappers, etc.

Example
SomeResource greetResource = RestClientBuilder.newBuilder()
                                 .baseUri(URI.create("http://localhost:8080"))
                                 .register(GreetClientRequestFilter.class)
                                 .register(GreetClientExceptionMapper.class)
                                 .build(GreetRestClient.class);
greetResource.getDefaultMessage();
Copied

Creating a New Client Using CDI

A client interface can be annotated with @RegisterRestClient to automatically register it with CDI. This annotation has a property called baseUri that can be used to define the base endpoint to be used by the client to access the service.

Example
@Path("/greet")
@RegisterRestClient(baseUri="http://localhost:8080")
public interface GreetRestClient {
    // ...
}
Copied

Any Jakarta REST (JAX-RS) providers for a client can be registered using the (repeatable) @RegisterProvider annotation on the interface as shown below.

Example
@Path("/greet")
@RegisterRestClient(baseUri="http://localhost:8080")
@RegisterProvider(GreetClientRequestFilter.class)
@RegisterProvider(GreetClientExceptionMapper.class)
public interface GreetRestClient {
    // ...
}
Copied

Once a client interface is annotated, it can be injected into any CDI bean.

All properties in annotation RegisterRestClient can be overridden via configuration as described in Configuration options

Example
public class MyBean {
    @Inject
    @RestClient
    GreetRestClient client;

    void myMethod() {
        client.getMessage("Helidon");
    }
}
Copied

Configuration

Configuration is only available for CDI managed client instances, it is not supported for client created programmatically using RestClientBuilder.

Most of the configuration properties mentioned below have to be prepended with the fully qualified classname of the client interface to be configured.

It is possible to avoid fully qualified classname by using @RegisterRestClient(configKey="clientAlias"), the prefix $restClient is used below to indicate an alias or a class name.

Configuration options

Required configuration options:

keytypedefault valuedescription
$restClient/mp-rest/urlstring Sets the base URL to use for this service. This option or /mp-rest/uri need to be set if the value is not present in RegisterRestClient#baseUri.
$restClient/mp-rest/uristring Sets the base URI to use for this service. This option or /mp-rest/url need to be set if the value is not present in RegisterRestClient#baseUri.

Optional configuration options:

keytypedefault valuedescription
$restClient/mp-rest/scopestringjakarta.enterprise.context.DependentThe fully qualified classname to a CDI scope to use for injection.
$restClient/mp-rest/connectTimeoutlong Sets timeout in milliseconds to wait to connect to the remote endpoint.
$restClient/mp-rest/readTimeoutlong Sets timeout in milliseconds to wait for a response from the remote endpoint.
$restClient/mp-rest/followRedirectsbooleanfalseSets value used to determine whether the client should follow HTTP redirect responses.
$restClient/mp-rest/proxyAddressstring Sets a string value in the form of <proxyHost>:<proxyPort> that specifies the HTTP proxy server hostname (or IP address) and port for requests of this client to use.
$restClient/mp-rest/queryParamStylestring (MULTI_PAIRS, COMMA_SEPARATED, ARRAY_PAIRS)MULTI_PAIRSSets enumerated type string value that specifies the format in which multiple values for the same query parameter is used.
$restClient/mp-rest/trustStorestring Sets the trust store location. Can point to either a classpath resource (e.g. classpath:/client-truststore.jks) or a file (e.g. file:/home/user/client-truststore.jks).
$restClient/mp-rest/trustStorePasswordstring Sets the password for the trust store.
$restClient/mp-rest/trustStoreTypestringJKSSets the type of the trust store.
$restClient/mp-rest/keyStorestring Sets the key store location. Can point to either a classpath resource (e.g. classpath:/client-keystore.jks) or a file (e.g. file:/home/user/client-keystore.jks).
$restClient/mp-rest/keyStorePasswordstring Sets the password for the keystore.
$restClient/mp-rest/keyStoreTypestringJKSSets the type of the keystore.
$restClient/mp-rest/hostnameVerifierstring Sets the hostname verifier class. This class must have a public no-argument constructor.

Configuration options affecting CDI and programmatically created clients:

keytypedefault valuedescription
$restClient/mp-rest/providersstring A comma separated list of fully-qualified provider classnames to include in the client.
$restClient/mp-rest/providers/<fully-qualified-provider-classname>/prioritystring Sets the priority of the provider for this interface.
org.eclipse.microprofile.rest.client.propagateHeadersstring To specify which headers to propagate from the inbound JAX-RS request to the outbound MP Rest Client request. Should not be prefixed with the rest client class or alias.
microprofile.rest.client.disable.default.mapperbooleanfalseWhether to disable default exception mapper. Should not be prefixed with the rest client class or alias.

Examples

To be able to run and test this example, use the Helidon MP examples/quickstarts. Add a dependency on the Helidon Rest Client implementation and create the following client interface:

client interface
@Path("/greet")
interface GreetRestClient {

     @GET
     JsonObject getDefaultMessage();

     @Path("/{name}")
     @GET
     JsonObject getMessage(@PathParam("name") String name);

}
Copied

Then create a runnable method as described in Creating new client, but with baseUri http://localhost:8080/greet and the above interface.

By calling GreetRestClient.getDefaultMessage() you reach the endpoint of Helidon quickstart.

Reference