spring-boot - 无法构建 `reactor.core.publisher.Mono` Spring Cloud OpenFeign 和 Spring boot 2 的实例

标签 spring-boot spring-cloud project-reactor

目标:从 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
}

问题: 我是否需要创建自己的 Single 到 Mono 转换器,或者这是 spring-cloud-starter-openfeign 的一些问题,所有这些都应该可以 OOTB?

最佳答案

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>-->

这解决了问题!

关于spring-boot - 无法构建 `reactor.core.publisher.Mono` Spring Cloud OpenFeign 和 Spring boot 2 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49299224/

相关文章:

spring-webflux - Spring webflux 错误处理程序 : How to get the reactor context of the request in the error handler?

spring-boot - API网关的认证与授权

java - Spring 启动 : no String-argument constructor/factory method to deserialize from String value

spring-boot - PrincipalExtractor 和 AuthoritiesExtractor 没有被调用

spring - 由 : java. lang.IllegalArgumentException 引起:无法解析值 'some.other.property' 中的占位符 "${some.other.property}"

java - Mono 与 CompletableFuture

java - 如何使用 Spring Boot 在 REST Web 服务中的 POST 请求期间修复 'HTTP-415' 错误

Spring Cloud Config Server 跨应用程序共享属性

java - NoSuchBeanDefinitionException : No qualifying bean of type 'javax.servlet.Filter' available

java - 项目 react 堆 : How to retry mono with different argument until some condition is met