为自定义构建器设置类

时间:2018-07-02 14:50:36

标签: java spring-webflux

我有一个要用于配置的自定义生成器:

RestClient client = RestClientBuilder.builder()
                    .gatewayUrl("http://localhost:8080")
                    .token("d778dh")
                    .usernamePassword("petrov", "password2")
                    .build();

自定义生成器:

public class RestClientBuilder {    ....

    public static RestClientBuilder builder() {
        return new RestClientBuilder();
    }

    public RestClientBuilder connectionTimeout(int milliseconds) {
        ReactorClientHttpConnector connector =
                new ReactorClientHttpConnector(options ->
                        options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000));

        return connector;
    }

    public RestClient build() throws SSLException {
        SslContext sslContext = sslContextBuilder != null ? sslContextBuilder.build() : null;
        return new RestClient(gatewayUrl.toString(), token, username, password, sslContext);
    }
}

我想使用以下代码来设置自定义超时:

ReactorClientHttpConnector connector =
            new ReactorClientHttpConnector(options ->
                    options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000));
WebClient webClient = WebClient.builder().clientConnector(connector).build();

如何在构建器中使用此自定义代码?我尝试了上面的示例,但是我必须使用Object Cast。还有其他解决方案吗?

1 个答案:

答案 0 :(得分:0)

RestClientRestClientBuilder不是Spring类,这就是为什么这些类不希望使用Spring WebFlux客户端连接器来配置它们的原因。

您可能正在寻找WebClient