How to send POST request by Spring cloud Feign

后端 未结 3 455
执念已碎
执念已碎 2020-12-29 12:14

It\'s my Feign interface

@FeignClient(
        name=\"mpi\",
        url=\"${mpi.url}\",
        configuration = FeignSimpleEncoderConfig.class
)
public inte         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 13:08

    It seems to me that Map is not valid for form body. MultiValueMap works just fine.

    Feign client:

    @FeignClient(name = "name", url="url", configuration = FromUrlEncodedClientConfiguration.class)
    public interface PayPalFeignClient {
    
        @RequestMapping(value = "/", method = POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
        @Headers("Content-Type: application/x-www-form-urlencoded")
        String foo(MultiValueMap formParams);
    }
    

    Config:

    @Configuration
    public class FromUrlEncodedClientConfiguration {
    
        @Autowired
        private ObjectFactory messageConverters;
    
        @Bean
        @Primary
        @Scope(SCOPE_PROTOTYPE)
        Encoder feignFormEncoder() {
            return new FormEncoder(new SpringEncoder(this.messageConverters));
        }
    }
    

    Gradle dependencies:

    compile group: 'io.github.openfeign.form', name: 'feign-form', version: '2.0.2'
    compile group: 'io.github.openfeign.form', name: 'feign-form-spring', version: '2.0.5'
    

    After that all you have to do is call it with a MultivalueMap parameter.

提交回复
热议问题