Interface RestClientBuilderProvider

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface RestClientBuilderProvider
Provides custom ClientBuilder configuration for creating REST clients.

Implementations of this interface can customize how Client instances are created for tests. This allows configuration of timeouts, SSL settings, custom providers, filters, and other client options.

Usage

Specify a custom provider using the @RestClientConfig qualifier on injected Client or WebTarget fields/parameters:

public class CustomTimeoutProvider implements RestClientBuilderProvider {
    @Override
    public ClientBuilder getClientBuilder() {
        return ClientBuilder.newBuilder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS);
    }
}

@RestBootstrap(MyApp.class)
public class MyTest {
    @RestResource
    @RestClientConfig(CustomTimeoutProvider.class)
    private Client client; // Will use custom timeouts

    @RestResource
    private Client defaultClient; // Will use default configuration
}

Global Provider via ServiceLoader

Instead of specifying the provider on each injection point, you can register a global provider via ServiceLoader. Create a file META-INF/services/dev.resteasy.junit.extension.api.RestClientBuilderProvider containing your implementation class name.

When a global provider is registered and no explicit @RestClientConfig is specified, the global provider will be used automatically for all injected Client instances.

SSL Support

When the test class is annotated with @SelfSignedCert, implementations can receive the generated certificate by declaring a field annotated with @SslCert:

public class CustomSslClientProvider implements RestClientBuilderProvider {
    @SslCert
    private SelfSignedCertificate certificate;

    @Override
    public ClientBuilder getClientBuilder() {
        return ClientBuilder.newBuilder()
                .sslContext(certificate.clientSslContext())
                .connectTimeout(5, TimeUnit.SECONDS);
    }
}

Requirements

  • Must have a public no-argument constructor
  • The getClientBuilder() method must return a non-null ClientBuilder
  • Can be registered globally via ServiceLoader or specified per-injection via @RestClientConfig
Since:
1.0
Author:
James R. Perkins
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    jakarta.ws.rs.client.ClientBuilder
    Provides a ClientBuilder for creating REST client instances.
  • Method Details

    • getClientBuilder

      jakarta.ws.rs.client.ClientBuilder getClientBuilder()
      Provides a ClientBuilder for creating REST client instances.

      The default implementation checks for a globally registered provider via ServiceLoader. If found, that provider's ClientBuilder is used. Otherwise, returns the default builder created by ClientBuilder.newBuilder().

      Implementations should override this method to provide custom client configuration such as timeouts, SSL settings, providers, or interceptors.

      Returns:
      the ClientBuilder to use for creating REST client instances, must not be null