NoSuchMethodError:基于 ANT 的 Spring 项目中的 reactor.netty.http.client.HttpClient.option

问题描述

我在使用 webClient 时遇到异常

Failed to instantiate [org.springframework.web.reactive.function.client.WebClient]: Factory method 'webClient' threw exception; nested exception is java.lang.NoSuchMethodError: reactor.netty.http.client.HttpClient.option(Lio/netty/channel/ChannelOption;Ljava/lang/Object;)Lreactor/netty/transport/Transport;

相关 jars

enter image description here

这是一个基于 ANT 的 SPRING 项目。 我没有收到任何编译错误

这是我创建 webClient 的代码

WebClient webClient(ReactiveClientRegistrationRepository  clientRegistrations ) {
    
    HttpClient httpClient = HttpClient.create()
              .option(ChannelOption.CONNECT_TIMEOUT_MILLIS,5000)
              .responseTimeout(Duration.ofMillis(5000))
              .doOnConnected(conn -> 
                conn.addHandlerLast(new ReadTimeoutHandler(5000,TimeUnit.MILLISECONDS))
                  .addHandlerLast(new WriteTimeoutHandler(5000,TimeUnit.MILLISECONDS)));
    
    ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);

    InMemoryReactiveOAuth2AuthorizedClientService authorisedClient=new InMemoryReactiveOAuth2AuthorizedClientService(clientRegistrations);
    
    ServerOAuth2AuthorizedClientExchangeFilterFunction oauth=new ServerOAuth2AuthorizedClientExchangeFilterFunction(new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(clientRegistrations,authorisedClient));
    oauth.setDefaultClientRegistrationId("id");
    return WebClient.builder().baseUrl("sample URL")
            .clientConnector(connector)
            .filter(oauth)
            .build();
}

id 和示例 URL 是我替换的值。 看起来像是一些 JAR 问题,但我添加了几乎所有我在研究此错误时发现的 JAR。

谢谢

解决方法

这取决于版本 Spring(和 Reactor Netty)。 在版本 5.2.x 和更高版本中,类 HttpClient 扩展了 ClientTransport - 在这种情况下,您的代码将起作用。 在早期版本中,documentation“提供”了这种实现:

HttpClient httpClient = HttpClient.create().tcpConfiguration(client -> client.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,10000))