将 Spring mvc 与 spring webflux 一起使用而不使用 spring 引导

Using Spring mvc with spring webflux without using spring boot

所以我有一个旧的工作应用程序,它使用 Spring MVC 而不是 spring 引导。我在我的项目中添加了 gradle 对 spring-webflux 的依赖,并开始使用 webclient 从我的项目中调用外部 APIs。这是我的网络客户端处理程序 class:

@Service
public class WebclientHandler {
    private final WebClient webClient;

    public WebclientHandler (WebClient.Builder builder) {
        this.webClient = builder.baseUrl("baseurl").build();
    }
}

现在我在通过我的 API:

调用此代码后收到此错误

Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.web.reactive.function.client.WebClient$Builder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

我的应用程序似乎无法找到 Webclient.builder 并且没有发生构造函数注入。这是因为我没有使用 spring 引导吗?

这是我的 Gradle 依赖项:

compile(group: 'org.springframework', name: 'spring-webflux', version: '5.3.15')

我是 spring-webflux 响应式编程的新手。

编辑

我也尝试使用配置创建 Webclient bean:

 @Configuration
    public class WebClientHandler {
    
    @Bean
        public WebClient myWebClient() {
            WebClient client = WebClient.builder().baseUrl("baseUrl")
                    .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                    .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
                    .build();
            return client;
        }
}

现在我在启动时遇到这个异常:

{ java.lang.IllegalStateException: No suitable default ClientHttpConnector found
    at org.springframework.web.reactive.function.client.DefaultWebClientBuilder.initConnector(DefaultWebClientBuilder.java:297)
    at org.springframework.web.reactive.function.client.DefaultWebClientBuilder.build(DefaultWebClientBuilder.java:266)
    at com.kronos.orgmap.sbs.impl.service.WebClientHandler.MyWebClient(WebClientHandler.java:41)
    at com.kronos.orgmap.sbs.impl.service.WebClientHandler$$EnhancerBySpringCGLIB$a9a582.CGLIB$desiWebClient[=16=](<generated>)
    at com.kronos.orgmap.sbs.impl.service.WebClientHandler$$EnhancerBySpringCGLIB$a9a582$$FastClassBySpringCGLIB$fa510d.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at com.kronos.orgmap.sbs.impl.service.WebClientHandler$$EnhancerBySpringCGLIB$a9a582.MyWebClient(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    at org.springframework.beans.factory.support.KronosConstructorResolver.instantiate(KronosConstructorResolver.java:654)
    at org.springframework.beans.factory.support.KronosConstructorResolver.instantiateUsingFactoryMethod(KronosConstructorResolver.java:487)
    at org.springframework.beans.factory.support.KronosAbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(KronosAbstractAutowireCapableBeanFactory.java:1345)
    at org.springframework.beans.factory.support.KronosAbstractAutowireCapableBeanFactory.createBeanInstance(KronosAbstractAutowireCapableBeanFactory.java:1193)
    at org.springframework.beans.factory.support.KronosAbstractAutowireCapableBeanFactory.doCreateBean(KronosAbstractAutowireCapableBeanFactory.java:611)
    at org.springframework.beans.factory.support.KronosAbstractAutowireCapableBeanFactory.createBean(KronosAbstractAutowireCapableBeanFactory.java:559)
    at org.springframework.beans.factory.support.KronosAbstractBeanFactory.lambda$doGetBean[=16=](KronosAbstractBeanFactory.java:336)
    at org.springframework.beans.factory.support.KronosDefaultSingletonBeanRegistry.getSingleton(KronosDefaultSingletonBeanRegistry.java:235)
    at org.springframework.beans.factory.support.KronosAbstractBeanFactory.doGetBean(KronosAbstractBeanFactory.java:334)
    at org.springframework.beans.factory.support.KronosAbstractBeanFactory.getBean(KronosAbstractBeanFactory.java:209)
    at org.springframework.beans.factory.support.KronosDefaultListableBeanFactory.preInstantiateSingletons(KronosDefaultListableBeanFactory.java:952)
    at com.kronos.container.impl.startup.KronosBeanFactory.preInstantiateSingletons(KronosBeanFactory.java:226)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:401)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:292)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4699)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:743)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:719)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1125)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1859)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
 } }

在我的 gradle 中添加这些依赖项为我解决了第二种方法的问题,现在 DefaultWebClientBuilder 能够启动 webclient builder 使用的连接器。

implementation platform('io.projectreactor:reactor-bom:2020.0.17')
    implementation 'io.projectreactor.netty:reactor-netty-core' 
    implementation 'io.projectreactor.netty:reactor-netty-http'

虽然仍然没有弄清楚为什么第一种方法在添加了提到的 gradle 依赖项后仍然不起作用。仍然无法在构造函数中找到 WebClient.Builder class 作为 bean。