Search code examples
spring-bootspring-cloudproject-reactor

Cannot construct instance of `reactor.core.publisher.Mono` Spring Cloud OpenFeign and Spring boot 2


Goal: migration from Spring Boot 1.x (webMvc) to version 2 (webFlux) and Spring Cloud Edgware SR2 to FinchleyM8 (awaiting release version).

Problem: Feign -> OpenFeign. OpenFeign under the hood uses RxJava but WebFlux - Reactor3. At the current moment when I using Mono as returned type, I have got an error:

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

Code example:

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

Question: Do I need to create my own converter Single to Mono or it's some problems of spring-cloud-starter-openfeign and all should work OOTB?


Solution

  • You can use those methods to adapt:

    For Single<T> rxJavaSingle

    Mono.from(RxReactiveStreams.toPublisher(rxJavaSingle))
    

    For Completable rxJavaCompletable

    Mono.from(RxReactiveStreams.toPublisher(rxJavaCompletable))