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).
<dependency>
<groupId>io.helidon.microprofile.rest-client</groupId>
<artifactId>helidon-microprofile-rest-client</artifactId>
</dependency>
API
| Class | Description |
|---|---|
org.eclipse.microprofile.rest.client.RestClientBuilder |
Base builder instance. Contains configuration options and a |
| Annotation | Description |
|---|---|
@RegisterRestClient |
A marker annotation to register a client at runtime. This marker must be applied to any CDI managed clients. |
@RestClient |
RestClient 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.
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.
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.
@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.
@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
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:
| key | type | default value | description |
|---|---|---|---|
|
string |
|
Sets the base URL to use for this service. This option or |
|
string |
|
Sets the base URI to use for this service. This option or |
Optional configuration options:
| key | type | default value | description |
|---|---|---|---|
|
string |
|
The fully qualified classname to a CDI scope to use for injection. |
|
long |
|
Sets timeout in milliseconds to wait to connect to the remote endpoint. |
|
long |
|
Sets timeout in milliseconds to wait for a response from the remote endpoint. |
|
boolean |
|
Sets value used to determine whether the client should follow HTTP redirect responses. |
|
string |
|
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. |
|
string (MULTI_PAIRS, COMMA_SEPARATED, ARRAY_PAIRS) |
|
Sets enumerated type string value that specifies the format in which multiple values for the same query parameter is used. |
|
string |
|
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). |
|
string |
|
Sets the password for the trust store. |
|
string |
|
Sets the type of the trust store. |
|
string |
|
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). |
|
string |
|
Sets the password for the keystore. |
|
string |
|
Sets the type of the keystore. |
|
string |
|
Sets the hostname verifier class. This class must have a public no-argument constructor. |
Configuration options affecting CDI and programmatically created clients:
| key | type | default value | description |
|---|---|---|---|
|
string |
|
A comma separated list of fully-qualified provider classnames to include in the client. |
|
string |
|
Sets the priority of the provider for this interface. |
|
string |
|
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. |
|
boolean |
|
Whether 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:
@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.