无法构造`reactor.core.publisher.Mono`实例 Spring Cloud OpenFeign和Spring Boot 2

3

目标:将Spring Boot 1.x (webMvc)迁移到版本2 (webFlux),并将Spring Cloud Edgware SR2迁移到FinchleyM8(等待发布版本)。

问题:Feign -> OpenFeign。OpenFeign在底层使用RxJava,但WebFlux- Reactor3。当前使用Mono作为返回类型时,会出现错误:

Caused by: org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class reactor.core.publisher.Mono]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of reactor.core.publisher.Mono (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

代码示例

@FeignClient(name = "thirdpartyresource", url = "${third.party.resource.url}")
public interface ThirdPartyResource {

    @PostMapping(value = "/validate", consumes = APPLICATION_FORM_URLENCODED_VALUE)
    Mono<ValidationResultDto> validate(MultiValueMap multiValueMap); // WORKS BAD
    // Single<ValidationResultDto> validate(MultiValueMap multiValueMap); WORKS WELL
}

问题: 我是否需要创建自己的单声道转换器,还是这是spring-cloud-starter-openfeign的某些问题,一切都应该开箱即用?


它不是开箱即用的。 - spencergibb
由于Spring默认提供Reactor 3,因此在使用Spring家族的项目时,使用它会更加合适。谢谢。 - Ruslan
正在处理 https://github.com/spring-cloud/spring-cloud-openfeign/pull/11 - spencergibb
谢谢,大家! - Ruslan
2个回答

3
reactor.core.publisher.Mono 是属于 spring-boot-starter-webflux jar 的一部分。
mvn repository 获取最新版本。
然后将其添加到 pom.xml 文件中。
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.5.3</version>
</dependency>

此外,为了避免不必要的麻烦,您需要从您的pom.xml中删除spring-boot-starter-web

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

这解决了问题!

0
你可以使用这些方法进行适应:
对于 Single<T> rxJavaSingle
Mono.from(RxReactiveStreams.toPublisher(rxJavaSingle))

对于 Completable rxJavaCompletable

Mono.from(RxReactiveStreams.toPublisher(rxJavaCompletable))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接