1、定义Feign的form编码器,让Feign支持form表单提交
package com.test.config;
import feign.Logger;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@RequiredArgsConstructor
@EnableFeignClients(basePackages = {"com.test.feign"})
@Configuration
public class OpenFeignConfig {
private final ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
2、定义feignclient
package com.test.feign.test;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
import java.util.Map;
@FeignClient(value = "test", url = "http://test.com")
public interface TestClient {
@PostMapping(value = "/test", consumes = "application/x-www-form-urlencoded;charset=UTF-8")
Result<List<TestUser>> test(Map<String,?> map);
@PostMapping(value = "/test", consumes = "application/x-www-form-urlencoded;charset=UTF-8")
Result<List<TestUser>> test1(TestReq req);
}