废话不多说直接贴源码
先导入如下两个依赖
<dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.2.2</version> </dependency> 需要创建一个配置文件
@Configuration public class FeignConfig { @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; @Bean public Encoder feignFormEncoder() { return new FeignSpringEncoder(messageConverters); } }
重写
SpringEncoder 我这里修改了逻辑
public class FeignSpringEncoder implements Encoder { private static final Log log = LogFactory.getLog(SpringEncoder.class); private final FeignSpringFormEncoder feignSpringFormEncoder = new FeignSpringFormEncoder(); private ObjectFactory<HttpMessageConverters> messageConverters; public FeignSpringEncoder(ObjectFactory<HttpMessageConverters> messageConverters) { this.messageConverters = messageConverters; } public void encode(Object requestBody, Type bodyType, RequestTemplate request) throws EncodeException { if (requestBody != null) { Class<?> requestType = requestBody.getClass(); Collection<String> contentTypes = (Collection)request.headers().get("Content-Type"); MediaType requestContentType = null; String message; if (contentTypes != null && !contentTypes.isEmpty()) { message &