maroki
maroki

Reputation: 23

Injecting feign client

I have an error when I inject feignClient interface in my service. This is a spring boot and spring cloud version that I use:

org.springframework.boot:spring-boot-starter-parent:2.0.6.RELEASE spring cloud version : Finchley.SR2

But when I create a feignclient bean in my class service it works.

Create a customer feign client:

@Component("DepartmentClient")
@FeignClient(name = "DEPARTMENT-SERVICE", url = "http://test")
public interface DepartmentClient {

    @RequestMapping(value = "/department/{departmentId}", method = RequestMethod.GET)
    void findDepartmetById(@PathVariable("departmentId") int departmentId);

}

I inject this feign client in the service like

@Service
public class AgentService {

    Logger logger = LoggerFactory.getLogger(AgentService.class);

    @Autowired
    private AgentRepository agentRepository;

    @Autowired
    private DepartmentClient departmentClient;
....
}

Output

Field departmentClient in ...AgentService required a bean of type '...DepartmentClient' that could not be found.
The injection point has the following annotations:
org.springframework.beans.factory.annotation.Autowired(required=true)
Action:

Consider defining a bean of type .... DepartmentClient' in your configuration.

Upvotes: 1

Views: 5973

Answers (4)

ThrowableException
ThrowableException

Reputation: 1426

I had a similar exception, But I already had @EnableFeignClients in my @SpringBootApplication class. still, spring was not able to create clientBean from FeignClient interface.

Turned out I had to provide basePackages value to the annotation. In absence of it, Spring doesn't create the LoadBalanced HTTP client class from @FeignClient Interface.

@EnableFeignClients(basePackages = "com.xxx.xxx")

Maybe because I always keep my ApplicationClass inside a config package and other code parallel to that package.

Upvotes: 3

denzal
denzal

Reputation: 1253

Adding more details below to the above answer :

In the @FeignClient annotation the String value ("department" above) is an arbitrary client name, which is used to create a Ribbon load balancer. You can also specify a URL using the url attribute (absolute value or just a hostname). The name of the bean in the application context is the fully qualified name of the interface. To specify your own alias value you can use the qualifier value of the @FeignClient annotation.

To make Feign client work, below are the steps we need to do:

1. Changes in the Feign Client : It should be an interface with Feign client annotation

@FeignClient(
  name = "DEPARTMENT-SERVICE",
  configuration = {DepartmentConfiguration.class},
  fallback = DepartmentFallback.class
)
@RequestMapping(
  value = {"${service.apipath.department}"},
  consumes = {"application/json"},
  produces = {"application/json"}
)
public interface DepartmentClient {

  @RequestMapping(value = "/department/{departmentId}", method = 
    RequestMethod.GET)
  void findDepartmetById(@PathVariable("departmentId") int departmentId);

}

2. Changes in the main class :

@EnableFeignClients
@SpringBootApplication
public class DepartmentApplication {
  public static void main(String[] args) {
    SpringApplication.run(DepartmentApplication.class, args);
  }
}

Upvotes: 0

Avi
Avi

Reputation: 1528

For Feign Client to work you have to add @EnableFeignClients to the Configuration class or @SpringBootApplication class

@SpringBootApplication
@EnableFeignClients
public class FooMain {  // Your Main class
    public static void main(String[] args) {
        SpringApplication.run(FooMain.class, args);
    }
}

Upvotes: 3

Alexandre
Alexandre

Reputation: 147

Have you tried to remove @Component of feign interface ?

Else look at you spring application component-scan, if your interface is not scan the bean will not be created

Upvotes: 0

Related Questions