从另一个项目注入FeignClient时出错 [英] Error Injecting FeignClient from another Project

查看:3047
本文介绍了从另一个项目注入FeignClient时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从其他项目自动连接假装客户端。似乎没有生成和注入假装客户端的实现。

I am having trouble auto wiring a feign client from another project. It appears that the implementation of the feign client is not being generated and injected.

这是我得到的错误。

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'passportRestController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private com.wstrater.service.contacts.client.ContactService com.wstrater.service.passport.server.controllers.PassportRestController.contactService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.wstrater.service.contacts.client.ContactService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

假装客户很直接。为简洁起见,我删除了导入。

The feign client is pretty straight forward. I have removed the imports for brevity.

package com.wstrater.service.contacts.client;

@FeignClient("contact-service")
public interface ContactService {

  @RequestMapping(method = RequestMethod.GET, value = ContactConstants.CONTACTS_USER_ID_PATH)
  public Collection<Contact> contactsByUserId(@PathVariable("userId") String userId);

}

我将组件扫描添加到我的项目中以包含应用程序它是控制器,并将假装客户端包含在另一个项目中。

I added the component scan to my project to include the application and it's controllers and to include the feign client in the other project.

package com.wstrater.service.passport.server;

@EnableEurekaClient
@EnableFeignClients
@SpringCloudApplication
@ComponentScan({"com.wstrater.service.passport.server",
                "com.wstrater.service.contacts.client"})
public class PassportServiceApplication {

  public static void main(String[] args) {
    ApplicationContext ctx = SpringApplication.run(PassportServiceApplication.class, args);
  }

}

剩下的大多数控制器为简洁起见,删除了导入。

The rest controller with most of the imports removed for brevity.

package com.wstrater.service.passport.server.controllers;

import com.wstrater.service.contacts.client.ContactService;

@RestController
public class PassportRestController {

  @Autowired
  private ContactService contactService;

  @RequestMapping(PassportContstants.PASSPORT_USER_ID_PATH)
  public ResponseEntity<Passport> passportByUserId(@PathVariable String userId) {
    ResponseEntity<Passport> ret = null;

    Collection<Contact> contacts = contactService.contactsByUserId(userId);
    if (contacts == null || contacts.isEmpty()) {
      ret = new ResponseEntity(HttpStatus.NOT_FOUND);
    } else {
      ret = ResponseEntity.ok(new Passport(contacts));
    }

    return ret;
  }

}

我试过定义feign客户端接口在不同的项目和不同的包中,只有当它与应用程序放在同一个包中时才能看到成功。这使得相信它是一个组件扫描问题,即使我在扫描中包含了包。我想将feign客户端接口保留在共享项目中,以定义可重用的契约,并为每个项目提供唯一的包结构,而不是使用应用程序使用它定义假装客户端。

I have tried defining the feign client interface in different projects and different packages and have only seen success when it put it in the same package as the application. This make be believe that it is a component scan issue even though I am including the package in the scan. I would like to keep the feign client interface in a shared project to define a reusable "contract" and for each project to have a unique package structure instead of defining the feign client with the application using it.

谢谢,Wes。

推荐答案

您需要告诉Feign扫描仪在哪里找到接口。

You need to tell the Feign scanner where to locate the interfaces.

您可以使用 @EnableFeignClients(basePackages = {my.external.feign.client.package,my.local.package} )

这篇关于从另一个项目注入FeignClient时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆