SpringBoot整合Feign
- 引入feign的maven配置
<!-- SpringCloud-feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
- 在启动类中添加注解 @EnableFeignClients(“feignClient的包名”)
@SpringBootApplication
@EnableScheduling
@EnableAsync
@EnableFeignClients("com.wf.service.feignClient")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 编写fegin请求接口
/**
* @Author: wwf
* @Description:
*/
@FeignClient(url = "${test.url}", name = "testClient")
public interface CenControllerClient {
@RequestMapping(value = "/test/menthod", method = RequestMethod.POST)
Result test(@RequestParam Integer orgId);
}
这里有两个需要注意的地方
一是@FeignClient后面两个参数:
url为服务器的地址
name为服务的名称
如果没有使用springcloud可以用这个方式直接请求其他服务器接口,本次不讨论微服务的请求方式;
二是请求参数注解的区分
如果使用的是参数形式使用 @RequestParam 注解。
如果使用的是对象的形式传递参数使用 @SpringQueryMap 注解。
- 以上配置完成之后就可以测试请求其他服务器的接口
--------------------------教程完毕,有问题评论交流