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

后端 未结 3 1252
盖世英雄少女心
盖世英雄少女心 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:43

    You must use FormEncoder in Feign encoder for url-form-encoded data in POST.

    Include the dependency to your app:

    Maven:

    
      io.github.openfeign.form
      feign-form
      3.8.0
    
    

    Add FormEncoder to your Feign.Builder like so:

    SomeFeign sample  = Feign.builder()
                          .encoder(new FormEncoder(new JacksonEncoder()))
                          .target(SomeFeign.class, "http://sample.test.org");
    

    In the Feign interface

    @RequestLine("POST /submit/form")
    @Headers("Content-Type: application/x-www-form-urlencoded")
    void from (@Param("field1") String field1, @Param("field2") String field2);
    

    Ref for more info: https://github.com/OpenFeign/feign-form

提交回复
热议问题