如何通过Spring cloud Feign发送POST请求
How to send POST request by Spring cloud Feign
这是我的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请求,我想我应该用编码器做更多的事情。
感谢您的帮助。
首先,您应该像这样更改您的 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();
}
}
在我看来,Map 对表单主体无效。 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));
}
}
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 参数调用它。
为句柄表单编码请求指定正确的编码器
你可以指定多编码器例子json/xml/formhttpurl编码
@Bean
public Encoder feignEncoder() {
ObjectFactory<HttpMessageConverters> objectFactory = () ->
new HttpMessageConverters(new FormHttpMessageConverter());
return new SpringEncoder(objectFactory);
}
重要的 FormHttpMessageConverter 仅序列化 MultiValueMap 子类
这是我的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请求,我想我应该用编码器做更多的事情。 感谢您的帮助。
首先,您应该像这样更改您的 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();
}
}
在我看来,Map 对表单主体无效。 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));
}
}
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 参数调用它。
为句柄表单编码请求指定正确的编码器
你可以指定多编码器例子json/xml/formhttpurl编码
@Bean
public Encoder feignEncoder() {
ObjectFactory<HttpMessageConverters> objectFactory = () ->
new HttpMessageConverters(new FormHttpMessageConverter());
return new SpringEncoder(objectFactory);
}
重要的 FormHttpMessageConverter 仅序列化 MultiValueMap 子类