这是我的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做更多的事情。 谢谢你的帮助。
答案 0 :(得分:3)
首先你要像这样改变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();
}
}
答案 1 :(得分:2)
在我看来,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);
}
配置:
@Configuration
public class FromUrlEncodedClientConfiguration {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
@Primary
@Scope(SCOPE_PROTOTYPE)
Encoder feignFormEncoder() {
return new FormEncoder(new SpringEncoder(this.messageConverters));
}
}
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'
之后,您只需使用MultivalueMap参数调用它。
答案 2 :(得分:0)
为句柄表格编码请求指定正确的编码器
您可以指定多编码器示例json / xml / formhttpurl encoded
Promise.all([getUserData(UserId)]).then((response) => {
var data = response[0];
var test = data.Item.streak;
console.log("Promise is " + test);
});
重要FormHttpMessageConverter仅序列化MultiValueMap子类