由于 java.lang.IllegalStateException 无法构造 Spring webClient:找不到合适的默认 ClientHttpConnector

Posted

技术标签:

【中文标题】由于 java.lang.IllegalStateException 无法构造 Spring webClient:找不到合适的默认 ClientHttpConnector【英文标题】:Cannot construct Spring webClient due to java.lang.IllegalStateException: No suitable default ClientHttpConnector found 【发布时间】:2020-11-06 00:36:23 【问题描述】:

我想在我的服务类中使用特定的WebClient。我在构造它时遇到了这个异常:

看起来这是一个依赖问题,但旧的干净安装并没有改变任何东西。

我是否需要在我的配置中手动指定一个 Bean?

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.***.Client]: Constructor threw exception; nested exception is java.lang.IllegalStateException: No suitable default ClientHttpConnector found
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:213)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:310)
        ... 122 more
    Caused by: java.lang.IllegalStateException: No suitable default ClientHttpConnector found
        at org.springframework.web.reactive.function.client.DefaultWebClientBuilder.getOrInitConnector(DefaultWebClientBuilder.java:266)
        at org.springframework.web.reactive.function.client.DefaultWebClientBuilder.build(DefaultWebClientBuilder.java:244)
        at com.***.Client.<init>(Client.kt:35)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at kotlin.reflect.jvm.internal.calls.CallerImpl$Constructor.call(CallerImpl.kt:41)
        at kotlin.reflect.jvm.internal.KCallableImpl.call(KCallableImpl.kt:106)
        at kotlin.reflect.jvm.internal.KCallableImpl.callDefaultMethod$kotlin_reflection(KCallableImpl.kt:152)
        at kotlin.reflect.jvm.internal.KCallableImpl.callBy(KCallableImpl.kt:110)
        at org.springframework.beans.BeanUtils$KotlinDelegate.instantiateClass(BeanUtils.java:788)
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:185)

我的课很简单:

@Service
class Client 
    private val webClient = WebClient.builder()
            .baseUrl("https://****.com/")
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .build()

在我的pom.xml 文件中:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webflux</artifactId>
        </dependency>

【问题讨论】:

【参考方案1】:

我发现了问题:原来 netty 依赖项丢失了,因为我导入了具有 WebClient 但没有其余部分的 maven 工件...

替换:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webflux</artifactId>
</dependency>

与:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

解决了。

【讨论】:

谢谢。我缺少 spring-boot-starter-webflux 依赖项 我已经添加了如上所述的依赖项,但问题仍然存在 你重新加载你的maven项目了吗? 这甚至解决了我在 wildfly 上的问题 io.projectreactor.netty:reactor-netty 以及 org.springframework:spring-webflux 依赖项对我有用。【参考方案2】:

作为 Léo Schenider 答案的替代方案,您无需将 spring-webflux 替换为 spring-boot-starter-webflux(添加了一些您不需要的额外依赖项),您只需要包括:

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

并保持 spring-webflux 不变。

【讨论】:

以上是关于由于 java.lang.IllegalStateException 无法构造 Spring webClient:找不到合适的默认 ClientHttpConnector的主要内容,如果未能解决你的问题,请参考以下文章

Collect failed in ... s 由于 Stage 由于 SparkContext 已关闭而取消

由于计算机积极拒绝

由于目标计算机积极拒绝,无法连接。

我在Android Studio工作,由于系统崩溃,一个或多个功能由于频繁的系统崩溃而崩溃。我应该如何恢复它?

SQL Server 服务由于登录失败而无法启动怎么办

Fiddler由于目标计算机积极拒绝,无法连接的解决方法