Webclient maven Dependency errors

人盡茶涼 提交于 2021-01-28 19:04:22

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/60730276/webclient-maven-dependency-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!