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

后端 未结 3 1199
盖世英雄少女心
盖世英雄少女心 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<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)
    }
    
    0 讨论(0)
  • 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:

    <dependency>
      <groupId>io.github.openfeign.form</groupId>
      <artifactId>feign-form</artifactId>
      <version>3.8.0</version>
    </dependency>
    

    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

    0 讨论(0)
  • 2021-02-05 16:44

    Full Java code with a simplified version of kazuar solution, works with Spring Boot:

    import java.util.Map;
    
    import feign.codec.Encoder;
    import feign.form.spring.SpringFormEncoder;
    
    import org.springframework.beans.factory.ObjectFactory;
    import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.cloud.openfeign.support.SpringEncoder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    
    import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE;
    
    @FeignClient(name = "srv", url = "http://s.com", configuration = Client.Configuration.class)
    public interface Client {
    
        @PostMapping(value = "/form", consumes = APPLICATION_FORM_URLENCODED_VALUE)
        void login(@RequestBody Map<String, ?> form);
    
        class Configuration {
    
            @Bean
            Encoder feignFormEncoder(ObjectFactory<HttpMessageConverters> converters) {
                return new SpringFormEncoder(new SpringEncoder(converters));
            }
        }
    }
    

    Dependency:

    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    
    0 讨论(0)
提交回复
热议问题