jerseyjax-rsresteasymicroprofile

How to select a RESTEasy Client Backend for the MicroProfile Rest Client?


Regarding Jersey, it allows the selection of the underlying Client Transport Connectors using a property [1], as shown in the following example:


Client client = ClientBuilder.newBuilder().
                    property(
                        ClientProperties.CONNECTOR_PROVIDER,
                        "some.package.to.XXXConnectorProvider"
                    ).
                    build();

This mechanism is also applicable to the MicroProfile Rest Client by setting this property in the RestClientBuilder, or even more flexibly by implementing the RestClientBuilderListener or RestClientListener and applying this configuration to the RestClientBuilder. [2],[3]

However, the RESTEasy documentation states that to select other backends [4], such as JettyClientEngine, ReactorNettyClientHttpEngine, and VertxClientHttpEngine, not only must the additional modules—org.jboss.resteasy:resteasy-client-jetty, org.jboss.resteasy:resteasy-client-reactor-netty, or org.jboss.resteasy:resteasy-client-vertx—be present in the classpath, but the client must also be manually built as follows:

ResteasyClient client = ((ResteasyClientBuilder)ClientBuilder.newBuilder()).
                            clientEngine(
                                new JettyClientEngine(new HttpClient())
                            ).
                            build();

Additionally, there is no guidance on how to select these backends when using the MicroProfile Rest Client[5].

Are there any ways to configure the RESTEasy Client to select its backend via a property or other method that is compatible with both the standard Jakarta RESTful Web Services Client API and the Eclipse MicroProfile Rest Client?

[1] https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest31x/client.html

[2] https://download.eclipse.org/microprofile/microprofile-rest-client-3.0/microprofile-rest-client-spec-3.0.html#programmatic_lookup

[3] https://download.eclipse.org/microprofile/microprofile-rest-client-3.0/microprofile-rest-client-spec-3.0.html#_provider_declaration

[4] https://docs.jboss.org/resteasy/docs/6.2.10.Final/userguide/#_transport_layer

[5] https://docs.jboss.org/resteasy/docs/6.2.10.Final/userguide/#_microprofile_rest_client


Solution

  • If you're building the client programmatically you can can use the register() method to register the client. Something like:

    CountriesServiceClient client = RestClientBuilder.newBuilder()
            .register(new JettyClientEngine(new HttpClient()))
            .baseUrl(new URL(serverHost))
            .build(CountriesServiceClient.class);
    

    It can be a little trickier, and maybe something that should be fixed, to do this for an injected client. However, something like this should work.

    public class JettyClientEngineProvider extends JettyClientEngine {
        public JettyClientEngineProvider() {
            super(new HttpClient());
        }
    }
    

    Then you can just add @RegisterProvider(JettyClientEngineProvider.class) to your client interface.

    I've also filed RESTEASY-3539 to better document this and add a better way to discover the client engine.