spring-bootmavenspring-webfluxspring-webclient

Webclient maven Dependency errors


I am getting a NoClassDefFoundError on the line where I try to create WebClient instance using 'create'. Tried builder() but still the same thing.

Please tell me what's wrong with the dependencies which I have added and how this issue can be solved.

             webClient = WebClient.create(url)
                                .post()
                                .uri(uri)
                                .contentType(MediaType.APPLICATION_JSON)
                                .body(BodyInserters.fromMultipartData(map))
                                .retrieve()
                                .bodyToMono(Object.class)
                                .block()
                                .toString()

dependencies which I added are

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
        </dependency>

StackTrace:

Exception in Async java.lang.NoClassDefFoundError: reactor/netty/http/client/HttpClient

java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: reactor/netty/http/client/HttpClient
        
        at org.springframework.http.client.reactive.ReactorClientHttpConnector.<clinit>(ReactorClientHttpConnector.java:44)
        
        at org.springframework.web.reactive.function.client.DefaultWebClientBuilder.initExchangeFunction(DefaultWebClientBuilder.java:226)
        
        at org.springframework.web.reactive.function.client.DefaultWebClientBuilder.build(DefaultWebClientBuilder.java:207)
        
        at org.springframework.web.reactive.function.client.WebClient.create(WebClient.java:144)

Caused by: java.lang.NoClassDefFoundError: reactor/netty/http/client/HttpClient
        
        ... 16 common frames omitted

Caused by: java.lang.ClassNotFoundException: reactor.netty.http.client.HttpClient
        
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
        
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:92)
        
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
        
        ... 16 common frames omitted


Solution

  • You need to include reactor-netty on the classpath. As by default that is the HTTP client used by WebClient.

    pom.xml:

    <dependency>
        <groupId>io.projectreactor.netty</groupId>
        <artifactId>reactor-netty</artifactId>
    </dependency>
    

    More info: https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-client