How to POST form-url-encoded data with Spring Cloud Feign

后端 未结 3 1197
盖世英雄少女心
盖世英雄少女心 2021-02-05 16:32

Using spring-mvc annotations, how can I define an @FeignClient that can POST form-url-encoded?

3条回答
  •  我在风中等你
    2021-02-05 16:36

    Use form encoder for feign: https://github.com/OpenFeign/feign-form and your feign configuration can look like this:

    class CoreFeignConfiguration {
    
      @Autowired
      private ObjectFactory messageConverters
    
      @Bean
      @Primary
      @Scope(SCOPE_PROTOTYPE)
      Encoder feignFormEncoder() {
          new FormEncoder(new SpringEncoder(this.messageConverters))
      }
    }
    

    Then, the client can be mapped like this:

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

提交回复
热议问题