java 从另一个项目注入 FeignClient 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30241198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:44:46  来源:igfitidea点击:

Error Injecting FeignClient from another Project

javaspring-cloudcomponent-scannetflix-feign

提问by Wes

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.

我在从另一个项目自动连接假客户端时遇到问题。似乎没有生成和注入 feign 客户端的实现。

This is the error I am getting.

这是我得到的错误。

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;
  }

}

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.

我尝试在不同的项目和不同的包中定义 feign 客户端界面,并且只有在将它与应用程序放在同一个包中时才看到成功。这让我相信这是一个组件扫描问题,即使我在扫描中包含了包。我想将 feign 客户端接口保留在共享项目中以定义可重用的“合同”,并为每个项目具有唯一的包结构,而不是使用使用它的应用程序定义 feign 客户端。

Thanks, Wes.

谢谢,韦斯。

回答by spencergibb

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

你需要告诉 Feign 扫描器在哪里找到接口。

You can use @EnableFeignClients(basePackages = {"my.external.feign.client.package", "my.local.package"}).

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

回答by Swarit Agarwal

Direct Class/Interface name can be given like below

直接类/接口名称可以如下所示

@EnableFeignClients(basePackageClasses=com.abc.xxx.client.XXFeignClient.class)

This parameter accept single or multiple class name

此参数接受单个或多个类名

回答by user12152065

My main class was in package "com.abc.myservicename" and my main class name was "myservicename.java". I was using @SpringBootApplication(scanBasePackages="com.abc") annotation in my main class.

我的主类在包“com.abc.myservicename”中,我的主类名称是“myservicename.java”。我在主类中使用 @SpringBootApplication(scanBasePackages="com.abc") 注释。

Changing the main class package name to "com.abc" has resolved the issue for me.

将主类包名称更改为“com.abc”已经为我解决了这个问题。