user1544460
user1544460

Reputation: 193

NoSuchMethodError: reactor.netty.http.client.HttpClient.option in ANT based Spring project

I am getting below exception while using 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;

Related JARS

enter image description here

This is a ANT based SPRING project. I dont get any compilation error.

This is the code where I am creating 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 and sample URL are values I replaced. Looks like its some JAR issue but I have added almost all Jars that I found researching this error.

Thanks

Upvotes: 1

Views: 4128

Answers (1)

Micle Demch.
Micle Demch.

Reputation: 31

It depends on the version Spring (and Reactor Netty). In version 5.2.x and later the class HttpClient extend ClientTransport - in this case your code will work. In earlier versions the documentation "offers" this implementation:

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

Upvotes: 3

Related Questions