如何通过Spring Cloud Feign发送POST请求

koa*_*a73 8 netflix-feign spring-cloud-feign spring-cloud-netflix

这是我的Feign界面

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

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<String> getPAReq(@QueryMap Map<String, String> queryMap
    );
}
Run Code Online (Sandbox Code Playgroud)

和我的自定义配置

public class FeignSimpleEncoderConfig {
    public static final int FIVE_SECONDS = 5000;

    @Bean
    public Logger.Level feignLogger() {
        return Logger.Level.FULL;
    }

    @Bean
    public Request.Options options() {
        return new Request.Options(FIVE_SECONDS, FIVE_SECONDS);
    }

    @Bean 
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        return Feign.builder()
                .encoder(new FormEncoder());
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我发送这样的请求,我会看到我的请求发送Content-Type:application / json; charset = UTF-8。但是如果我设置内容类型

consumes = "application/x-www-form-urlencoded"
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息

feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap] and content type [application/x-www-form-urlencoded]
    at org.springframework.cloud.netflix.feign.support.SpringEncoder.encode(SpringEncoder.java:108) ~[spring-cloud-netflix-core-1.1.7.RELEASE.jar:1.1.7.RELEASE]
Run Code Online (Sandbox Code Playgroud)

如何发送POST请求,我想我应该使用Encoder做更多的事情。谢谢你的帮助。

koa*_*a73 6

首先,您应该像这样更改 Feign 界面:

@FeignClient (
    configuration = FeignSimpleEncoderConfig.class
)
public interface MpiClient {
   @RequestMapping(method = RequestMethod.POST)
   ResponseEntity<String> getPAReq(Map<String, ?> queryMap);
}
Run Code Online (Sandbox Code Playgroud)

然后你应该在 feign 配置期间设置编码器:

public class FeignSimpleEncoderConfig {
    @Bean
    public Encoder encoder() {
        return new FormEncoder();
    }
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*kár 5

在我看来 Map 对表单体无效。MultiValueMap 工作得很好。

Feign客户端:

@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<String, ?> formParams);
}
Run Code Online (Sandbox Code Playgroud)

配置:

@Configuration
public class FromUrlEncodedClientConfiguration {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    @Primary
    @Scope(SCOPE_PROTOTYPE)
    Encoder feignFormEncoder() {
        return new FormEncoder(new SpringEncoder(this.messageConverters));
    }
}
Run Code Online (Sandbox Code Playgroud)

Gradle 依赖项:

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'
Run Code Online (Sandbox Code Playgroud)

之后,您所要做的就是使用 MultivalueMap 参数调用它。