Chinaunix首页 | 论坛 | 博客
  • 博客访问: 535087
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-02-25 14:40:48


点击(此处)折叠或打开


  1. 由于只想在SpringBoot中使用一下Feign客户端,来访问第三方请求,但因为各种版本问题,一直报各种乱七八糟的错

  2. pom文件
  3. <parent>
  4.     <groupId>org.springframework.boot</groupId>
  5.     <artifactId>spring-boot-starter-parent</artifactId>
  6.     <version>2.1.12.RELEASE</version>
  7.     <relativePath/> <!-- lookup parent from repository -->
  8. </parent>
  9.  
  10. <dependencies>
  11.     <dependency>
  12.         <groupId>org.springframework.boot</groupId>
  13.         <artifactId>spring-boot-starter-web</artifactId>
  14.     </dependency>
  15.     <dependency>
  16.         <groupId>org.projectlombok</groupId>
  17.         <artifactId>lombok</artifactId>
  18.         <version>1.16.20</version>
  19.         <scope>provided</scope>
  20.     </dependency>
  21.     <dependency>
  22.         <groupId>org.springframework.cloud</groupId>
  23.         <artifactId>spring-cloud-starter-openfeign</artifactId>
  24.         <version>2.0.2.RELEASE</version>
  25.     </dependency>
  26. </dependencies>

  27. 注意这里的springboot的版本号和openfeign的版本号非常重要,不要盲目使用最新版本

  28. 然后是常规操作

  29. 在项目启动类上添加@EnableFeignClients注解
  30. //from fhadmin.cn
  31. @EnableFeignClients
  32. @SpringBootApplication
  33. public class ClientApplication {
  34.     public static void main(String[] args) {
  35.         SpringApplication.run(ClientApplication.class,args);
  36.     }
  37. }

  38. 如果忘记添加该注解,会报自己定义的FeignClient注入不进去的错

  39. 编写接口Interface写FeignClient
  40. 格式大致像如下方式

  41. @FeignClient(
  42.         name = "userService",
  43.         url = ""
  44. )
  45. //from fhadmin.cn
  46. public interface UserFeignClient {
  47.     @RequestMapping(value = "/getUserInfo",method = RequestMethod.GET)
  48.     @ResponseBody
  49.     String getUserInfo();
  50. }
  51. 如果未为FeignClient设置name,也会报错,会报一个没有name的错,一看就能明白


阅读(493) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~