Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Using spring-mvc annotations:

  • How can I define an @FeignClient that can POST form-url-encoded?
like image 954
Newbie Avatar asked Mar 04 '16 18:03

Newbie


1 Answers

Use FormEncoder for Feign:

  • https://github.com/OpenFeign/feign-form

And your Feign configuration can look like this:

class CoreFeignConfiguration {
  @Autowired
  private ObjectFactory<HttpMessageConverters> 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<String, ?> formParams)
}
like image 64
kazuar Avatar answered Sep 28 '22 23:09

kazuar