参考文章:
https://stackoverflow.com/questions/35803093/how-to-post-form-url-encoded-data-with-spring-cloud-feign
1:首先加一个配置文件 CoreFeignConfiguration,如下
package xxxx;
import feign.codec.Encoder;
import feign.form.FormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;
public class CoreFeignConfiguration {
@Autowired
private ObjectFactory messageConverters;
@Bean
@Primary
@Scope(SCOPE_PROTOTYPE)
Encoder feignFormEncoder() {
return new FormEncoder(new SpringEncoder(this.messageConverters));
}
}
你的feign client 像如下的东西
@FeignClient(
name = “eca-api”,
url = “${url.feign.eca-api}”,
configuration = CoreFeignConfiguration.class,
fallbackFactory = EcaApiClientFallbackFactory.class
)
public interface EcaApiClient {
@RequestMapping(value = “/open-api/token”, method = POST,
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
@Headers(“Content-Type: application/x-www-form-urlencoded”)
ECAR getToken(@RequestHeader(name = “Authorization”) String secret,
Map<String, ?> formParams);
}