Skip to content

Rest Client

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.

You can also use metrics annotations on your Rest Client methods as described in this related page.

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).

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

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

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

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

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

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

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

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

Example

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

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

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

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

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/urlstringSets 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/uristringSets 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/connectTimeoutlongSets timeout in milliseconds to wait to connect to the remote endpoint.
$restClient/mp-rest/readTimeoutlongSets 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/proxyAddressstringSets 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/trustStorestringSets 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/trustStorePasswordstringSets the password for the trust store.
$restClient/mp-rest/trustStoreTypestringJKSSets the type of the trust store.
$restClient/mp-rest/keyStorestringSets 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/keyStorePasswordstringSets the password for the keystore.
$restClient/mp-rest/keyStoreTypestringJKSSets the type of the keystore.
$restClient/mp-rest/hostnameVerifierstringSets 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/providersstringA comma separated list of fully-qualified provider classnames to include in the client.
$restClient/mp-rest/providers/<fully-qualified-provider-classname>/prioritystringSets the priority of the provider for this interface.
org.eclipse.microprofile.rest.client.propagateHeadersstringTo 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

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

    @GET
    JsonObject getDefaultMessage();

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

}

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