springcloud 使用feign创建client接口 ,controller引入后报依赖错误 Unsatisfied dependency

如题所示,代码如下

 1 -- 1. client
 2 @FeignClient(name="microservice-provider-user")
 3 public interface UserFeignClient {
 4   @GetMapping("/users/{id}")
 5   User findById(@PathVariable Long id);
 6 }
 7 
 8 -- 2. 控制层
 9 @RequestMapping("/movies")
10 @RestController
11 public class MovieController {
12     @Autowired
13     private RestTemplate restTemplate;
14 
15     @Autowired
16     private UserFeignClient userFeignClient;
17 
18     @GetMapping("/users/{id}")
19     public User findById(@PathVariable Long id) {
20         // 这里用到了RestTemplate的占位符能力
21         User user = userFeignClient.findById(id);
22         // ...电影微服务的业务...
23         return user;
24     }
25 }
26 
27 -- 3. 启动类
28 @SpringBootApplication
29 @EnableEurekaClient
30 @EnableFeignClients("com.caiya.movie.utils")
31 public class FeignMovieApplication {
32     @Bean
33     public RestTemplate restTemplate() {
34         return new RestTemplate();
35     }
36 
37     public static void main(String[] args) {
38         SpringApplication.run(FeignMovieApplication.class, args);
39     }
40 }

 

百度了下,得到的结果是

  1. springboot 启动类注解 @EnableFeignClients 没有写扫描包 (对比笔者自己的工程,client是在 启动类所在包的子包下,理论上不是这个问题
不过还是按提示修改后,一样报错...

  2. 笔者自己设置了多个模块,包名存在一致的情况,为了排除这个情况导致,修改包名后还是一样的问题

最终解决方案:
  还是 stackoverflow 牛皮,google一下就出来答案了

原因是我在复制网上教程时候,直接把 @PathVariable("id") 里面的id省略了, 自以为id可写可不写

@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@GetMapping("/users/{id}")
  User findById(@PathVariable("id") Long id); // 此处(“id”)必写
}

不过这是feign的bug,后续的版本好像是解决了

解答原文及地址: https://stackoverflow.com/questions/49415197/spring-boot-2-unsatisfied-dependency-on-feign-client-when-autowired-for-servic

There was an issue in feign client before. I guess you are experiencing the same maybe because you are using an old version but what you should do is including the pathVariable name in your @PathVariable annotation like this 
    @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
    String hello(@PathVariable(value = "name") String name);

You can find the details from 
https://github.com/spring-cloud/spring-cloud-netflix/issues/861

  

 



 

 

 

 

posted @ 2020-11-21 12:16  菜牙caiya  阅读(560)  评论(0)    收藏  举报