依赖:
<properties>
<java.version>1.8</java.version>
<feign-core.version>10.2.0</feign-core.version>
<feign-form.version>2.1.0</feign-form.version>
</properties>
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-core</artifactId> <version>${feign-core.version}</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-jackson</artifactId> <version>${feign-core.version}</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>${feign-form.version}</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>${feign-form.version}</version> </dependency>
或者直接使用依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
配置:
@Configuration @ComponentScan(basePackages = "com.xxx.subscribe.milestone.command.web.job") public class XxlJobConfig { /* @Autowired private ObjectFactory<HttpMessageConverters> messageConverters*/; private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class); @Value("${xxl.job.admin.addresses}") private String adminAddresses;// http:127.0.0.1:8888 //define feign client @Bean public XxService xxService() { return Feign.builder() // .encoder(new FormEncoder(new JacksonEncoder())) //如果有表单提交,加上表单编解码 // .decoder(new JacksonDecoder()) // .target(XxlJobService.class, adminAddresses); } }
XxService:
import com.alibaba.fastjson.JSONObject; import feign.Headers; import feign.Param; import feign.RequestLine; import java.util.Map; public interface XxlJobService { @RequestLine("GET /job-admin/jobinfo/pageList?jobGroup={jobGroup}") JSONObject pageList(@Param(value = "jobGroup") int jobGroup); @RequestLine("POST /job-admin/jobinfo/add") @Headers("Content-Type: application/x-www-form-urlencoded") JSONObject add(Map<String, ?> form); @RequestLine("GET /job-admin/jobinfo/start?id={id}") JSONObject start(@Param(value = "id") int id); @RequestLine("GET /job-admin/jobinfo/stop?id={id}") JSONObject stop(@Param(value = "id") int id); }