11

这是我的 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
    );
}

和我的自定义配置

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());
    }
}

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

consumes = "application/x-www-form-urlencoded"

我有这个错误信息

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]

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

4

3 回答 3

6

首先你应该像这样改变你的 Feign 界面:

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

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

public class FeignSimpleEncoderConfig {
    @Bean
    public Encoder encoder() {
        return new FormEncoder();
    }
}
于 2017-02-24T17:11:57.523 回答
5

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

假装客户端:

@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);
}

配置:

@Configuration
public class FromUrlEncodedClientConfiguration {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    @Primary
    @Scope(SCOPE_PROTOTYPE)
    Encoder feignFormEncoder() {
        return new FormEncoder(new SpringEncoder(this.messageConverters));
    }
}

摇篮依赖:

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'

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

于 2017-08-31T08:33:26.777 回答
0

为处理表单编码请求指定正确的编码器

您可以指定多编码器示例 json/xml/formhttpurl 编码

@Bean
public Encoder feignEncoder() {
    ObjectFactory<HttpMessageConverters> objectFactory = () ->
            new HttpMessageConverters(new FormHttpMessageConverter());
    return new SpringEncoder(objectFactory);
}

重要的 FormHttpMessageConverter 仅序列化 MultiValueMap 子类

于 2018-04-09T17:16:00.087 回答