Error Injecting FeignClient from another Project
Asked Answered
V

6

48

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.

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.

Thanks, Wes.

Victoria answered 14/5, 2015 at 15:27 Comment(0)
C
106

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

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

Codd answered 14/5, 2015 at 18:13 Comment(5)
Thanks. I gave that a quick try since something else just came up but I am not seeing a difference. I moved the package list from the ComponentScan to the EnableFeignClients. @EnableFeignClients(basePackages = {"com.wstrater.service.passport.server", "com.wstrater.service.contacts.client"}). It is still getting the error.Victoria
I think I was having issues somewhere between seat and keyboard because it is working now. Thanks.Victoria
Thanks @Victoria this helped me out.Thiel
This helped me a lot. Thank you!Luu
In my test class, IntelliJ tells me there is an error but if I run the test it will run just fine. I have to annotate the test class with @SuppressWarnings("SpringJavaAutowiringInspection") and it will suppress this error warnings.Bombe
G
2

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

Glabella answered 27/11, 2017 at 11:9 Comment(0)
S
1

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.

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

Slop answered 2/10, 2019 at 5:53 Comment(0)
G
1

This worked for me. Adding annotation to main class

@EnableFeignClients(defaultConfiguration = {FeignBasicConfiguration.class}, basePackages = {"com.example"})
Gony answered 23/11, 2022 at 3:58 Comment(0)
P
0

Thanks for the help. I added the right external package in @EnableFeignClients(basePackages = {}) It still did not pick up the feign client implementation.

I had a PACT contract test where I was autowiring the api client bean. I had used @ExtendWith(SpringExtension.class) which was the issue. I replaced with @SpringBootTest, which allowed the feign client bean to be exposed in this class

Peduncle answered 5/8, 2021 at 15:26 Comment(0)
I
0

Faced the Same issue where i was not able to @Autowire the FeignClient interface, Changing the Version of Feign Client in pom.xml to 3.0.1 or latest has helped me in resolving the issue.

Improvident answered 28/7, 2022 at 7:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.