【问题标题】:Problem deserialize flux/mono into Feign Spring Cloud将flux/mono反序列化为Feign Spring Cloud的问题
【发布时间】:2019-05-29 07:45:55
【问题描述】:

我使用 Kotlin Webflux (Reactor3)、Eureka、Zuul 和 Feign 开发了一个微服务应用程序。除了通过我的微服务 Feign 调用 API 时总是出错。看起来他无法反序列化数据。你能告诉我 Feign 是否与 Flux 和 Monno 兼容? 谢谢你

{ "时间戳": "2019-05-29T07:39:43.998+0000", "路径": "/爱好/", “状态”:500, "error": "内部服务器错误", “消息”:“类型定义错误:[简单类型,类 reactor.core.publisher.Flux];嵌套异常是 com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造 reactor.core.publisher.Flux 的实例(没有创作者,如默认构造,存在):抽象类型要么需要映射到具体类型,具有自定义反序列化器,要么在 [Source: (PushbackInputStream); line: 1, column: 1] 处包含其他类型信息\n }

【问题讨论】:

    标签: spring-webflux project-reactor netflix-zuul spring-cloud-netflix feign


    【解决方案1】:

    Feign 不支持 Mono/Flux 反序列化。存在完全支持它的替代 feign 库:feign-reactive

    请注意,这是对 feign 的重写,它完全使用响应式代码,与 OpenFeign 的 Feign 核心不同。

    这里有一个关于如何使用它以及普通 Feign 的 sn-p,取自 sample app

    @SpringBootApplication(exclude = ReactiveLoadBalancerAutoConfiguration.class)
    @RestController
    @EnableReactiveFeignClients
    @EnableFeignClients
    public class FeignApplication {
    
        @Autowired
        private GreetingReactive reactiveFeignClient;
    
        @Autowired
        private Greeting feignClient;
    
        public static void main(String[] args) {
            SpringApplication.run(FeignApplication.class, args);
        }
    
        @GetMapping("/greetingReactive")
        public Mono<String> greetingReactive() {
            return reactiveFeignClient.greeting().map(s -> "reactive feign! : " + s);
        }
    
        @GetMapping("/greeting")
        public String greeting() {
            return "feign! : " + feignClient.greeting();
        }
    }
    

    【讨论】:

    • 但我无法反序列化 ResponseEntity。这是错误:引起:org.springframework.core.codec.CodecException:类型定义错误:[简单类型,类org.springframework.http.ResponseEntity];嵌套异常是 com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造 org.springframework.http.ResponseEntity 的实例(没有创建者,如默认构造,存在):无法从对象值反序列化(没有基于委托或属性的创建者)跨度>
    • 我遇到了同样的错误@R.I,你解决了吗?
    【解决方案2】:

    除了 Adhika Setya Pramudita 响应之外,我想提一下,为了在控制器中返回 Mono,您必须使用 Spring WebFlux 而不是 Spring MVC

    【讨论】:

      【解决方案3】:

      我无法让@Adhika Setya Pramudita 解决方案工作,直觉告诉我,由于混合了@EnableReactiveFeignClients,它甚至无法运行@ @EnableFeignClients 需要对应的 @EnableWebFlux @EnableWebMvc,因此定义两者可能会编译但会在运行时失败。

      由于 op 没有提及目标语言,我想分享适用于我的 Kotlin 设置:

      build.gradle.kts

      implementation("org.springframework.boot:spring-boot-starter-webflux")
      implementation("com.playtika.reactivefeign:feign-reactor-core:2.0.22")
      implementation("com.playtika.reactivefeign:feign-reactor-spring-configuration:2.0.22")
      implementation("com.playtika.reactivefeign:feign-reactor-webclient:2.0.22")
      

      Config.kt

      @Configuration
      @EnableWebFlux
      @EnableReactiveFeignClients
      class Config {
      }
      

      MyEntity.kt

      class MyEntity @JsonCreator constructor(
              @param:JsonProperty("my_value") val my_value: String
      )
      

      MyFeignClient.kt

      @Component
      @ReactiveFeignClient(
              url = "\${package.service.my-service-url}",
              name = "client"
      )
      interface MyFeignClient {
          @GetMapping(value = ["/my/url?my_param={my_value}"], consumes = ["application/json"])
          fun getValues(
                  @PathVariable(name = "my_value") myValue: String?,
              ): Mono<MyEntity?>?
      }
      

      然后这里是一些服务类中的代码:

      val myClient: MyFeignClient = WebReactiveFeign.builder<MyFeignClient>()
              .contract(ReactiveContract(SpringMvcContract()))
              .target(MyFeignClient::class.java, "http://example.com")
      
      // feel free to add .block() to get unpacked value or just chain your logic further
      val response = myClient.getValues(param)
      

      【讨论】:

        猜你喜欢
        • 2021-09-30
        • 2021-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-05
        • 2021-05-11
        • 2023-01-28
        • 2017-10-27
        相关资源
        最近更新 更多