使用spring-mvc批注,如何定义可以以POST形式URL编码的@FeignClient?

最佳答案

将表单编码器用于伪装:https://github.com/OpenFeign/feign-form,伪装配置如下所示:

class CoreFeignConfiguration {

  @Autowired
  private ObjectFactory<HttpMessageConverters> messageConverters

  @Bean
  @Primary
  @Scope(SCOPE_PROTOTYPE)
  Encoder feignFormEncoder() {
      new FormEncoder(new SpringEncoder(this.messageConverters))
  }
}


然后,可以像这样映射客户端:

@FeignClient(name = 'client', url = 'localhost:9080', path ='/rest', configuration = CoreFeignConfiguration)
interface CoreClient {

    @RequestMapping(value = '/business', method = POST, consumes = MediaType.APPLICATION_FORM_URLENCODED)
    @Headers('Content-Type: application/x-www-form-urlencoded')
    void activate(Map<String, ?> formParams)
}

07-25 23:58
查看更多