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

Léo*_*der 12 kotlin spring-boot

我想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)
Run Code Online (Sandbox Code Playgroud)

我的课很简单:

@Service
class Client {
    private val webClient = WebClient.builder()
            .baseUrl("https://****.com/")
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .build()
}
Run Code Online (Sandbox Code Playgroud)

在我的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>
Run Code Online (Sandbox Code Playgroud)

Léo*_*der 30

我发现了问题:事实证明缺少 netty 依赖项,因为我导入了具有WebClient但没有其余部分的 Maven 工件...

更换:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webflux</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

和:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

解决了。

  • `io.projectreactor.netty:reactor-netty` 和 `org.springframework:spring-webflux` 依赖项对我有用。 (3认同)

小智 18

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

\n
<dependency>\n    <groupId>io.projectreactor.netty</groupId>\n    <artifactId>reactor-netty-http</artifactId>\n</dependency>\n
Run Code Online (Sandbox Code Playgroud)\n

并保留 spring-webflux 不变。

\n