如何通过Spring Cloud Feign发送POST请求 [英] How to send POST request by Spring cloud Feign
本文介绍了如何通过Spring Cloud Feign发送POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的Feign界面
It's my Feign interface
@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. 但是,如果我设置内容类型
If I send request like this I see that my request send Content-Type: application/json;charset=UTF-8. But if I set content type
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做更多的事情. 感谢您的帮助.
How to send POST request, I think I should make something more with Encoder. Thanks for your help.
推荐答案
首先,您应该像这样更改Feign接口:
First of all you should change your Feign interface like this:
@FeignClient (
configuration = FeignSimpleEncoderConfig.class
)
public interface MpiClient {
@RequestMapping(method = RequestMethod.POST)
ResponseEntity<String> getPAReq(Map<String, ?> queryMap);
}
然后,您应在伪装配置期间设置编码器:
Then you should set the encoder during feign configuration:
public class FeignSimpleEncoderConfig {
@Bean
public Encoder encoder() {
return new FormEncoder();
}
}
这篇关于如何通过Spring Cloud Feign发送POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文