Spring boot application not able to find bean for feign client
Asked Answered
A

10

8

Getting below exception while testing feign functionality.
*********************
APPLICATION FAILED TO START
*********************

Description:

Field currencyConversionServiceProxy in com.in28minutes.microservices.currencyconversionservice.CurrencyConversionController required a bean of type 'com.in28minutes.microservices.currencyconversionservice.CurrencyConversionServiceProxy' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)

POM.xml

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
....
<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

 <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

CurrencyConversionServiceApplication.java

@SpringBootApplication
@EnableFeignClients
public class CurrencyConversionServiceApplication {

  public static void main(String[] args) {
     SpringApplication.run(CurrencyConversionServiceApplication.class, args);
  }
}

CurrencyConversionController.java

@RestController
public class CurrencyConversionController {

  @Autowired
  private CurrencyConversionServiceProxy currencyConversionServiceProxy;

  @GetMapping("/currency-converter-feign/from/{from}/to/{to}/quantity/{quantity}")
  public CurrencyConversionBean convertCurrencyFeign(@PathVariable String from, @PathVariable String to,
        @PathVariable BigDecimal quantity) {

      CurrencyConversionBean response = currencyConversionServiceProxy.retrieveExchangeValue(from, to);
      return new CurrencyConversionBean(response.getId(), response.getFrom(), response.getTo(),
            response.getConversionMultiple(), quantity, quantity.multiply(response.getConversionMultiple()),
            response.getPort());
  }
}

CurrencyConversionServiceProxy.java

@FeignClient(name="currency-exchange-service", url="localhost:8000")
public interface CurrencyConversionServiceProxy {

  @GetMapping("/currency-exchange/from/{from}/to/{to}")
  public CurrencyConversionBean retrieveExchangeValue(@PathVariable("from") String from, @PathVariable("to") String to);

}

Logs

      .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.2.RELEASE)

2019-12-28 14:19:27.788  INFO 11444 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : 
Fetching config from server at : http://localhost:8888
2019-12-28 14:19:28.805  INFO 11444 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : 
Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2019-12-28 14:19:28.805  WARN 11444 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : 
Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/currency- 
conversion-service/default": Connection refused: connect; nested exception is 
java.net.ConnectException: Connection refused: connect
2019-12-28 14:19:28.805  INFO 11444 --- [  restartedMain] m.c.CurrencyConversionServiceApplication : 
No active profile set, falling back to default profiles: default
2019-12-28 14:19:28.921  INFO 11444 --- [  restartedMain] o.s.cloud.context.scope.GenericScope     : 
BeanFactory id=6fe0f2bd-0b38-367c-af3b-d79d3b2d9d52
2019-12-28 14:19:28.937  WARN 11444 --- [  restartedMain] org.apache.tomcat.util.modeler.Registry  : 
The MBean registry cannot be disabled because it has already been initialised
2019-12-28 14:19:28.968  INFO 11444 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : 
Tomcat initialized with port(s): 8100 (http)
2019-12-28 14:19:28.968  INFO 11444 --- [  restartedMain] o.apache.catalina.core.StandardService   : 
Starting service [Tomcat]
2019-12-28 14:19:28.984  INFO 11444 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : 
Starting Servlet engine: [Apache Tomcat/9.0.29]
2019-12-28 14:19:28.984  INFO 11444 --- [  restartedMain] o.a.c.c.C.[Tomcat-16].[localhost].[/]    : 
Initializing Spring embedded WebApplicationContext
2019-12-28 14:19:28.984  INFO 11444 --- [  restartedMain] o.s.web.context.ContextLoader            : 
Root WebApplicationContext: initialization completed in 178 ms
2019-12-28 14:19:28.999  WARN 11444 --- [  restartedMain] ConfigServletWebServerApplicationContext : 
Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
'currencyConversionController': Unsatisfied dependency expressed through field 
'currencyConversionServiceProxy'; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 
'com.in28minutes.microservices.currencyconversionservice.CurrencyConversionServiceProxy' available: 
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
2019-12-28 14:19:29.015  INFO 11444 --- [  restartedMain] o.apache.catalina.core.StandardService   : 
Stopping service [Tomcat]
2019-12-28 14:19:29.015  INFO 11444 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 
'debug' enabled.
2019-12-28 14:19:29.093 ERROR 11444 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field currencyConversionServiceProxy in 
com.in28minutes.microservices.currencyconversionservice.CurrencyConversionController required a bean 
of type 'com.in28minutes.microservices.currencyconversionservice.CurrencyConversionServiceProxy' 
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 
'com.in28minutes.microservices.currencyconversionservice.CurrencyConversionServiceProxy' in your 
configuration.

I have tested service running at port 8000 is running fine under name "currency-exchange-service" only.

Armlet answered 28/12, 2019 at 9:44 Comment(5)
I have checked related query, but it not working. #51485679Armlet
CurrencyConversionServiceProxy is an interface - where is its implementation? Have the correct annotations like Qualifier been used?Endorsement
@FeignClient itself create the implementation for this interface. Below definition I have found from its documnetation. "Annotation for interfaces declaring that a REST client with that interface should becreated (e.g. for autowiring into another component). " javadoc.io/doc/org.springframework.cloud/…Armlet
Changing the Spring cloud version to "Finchley.RELEASE" working for me. But I'm not able to understand why it is not working in version "Hoxton.SR1"Armlet
Hoxton.SR1 is a very new release, so that might explain issues you're having. Glad to hear you got it sorted!Endorsement
P
15

Try to specify a class name of a Feign client into @EnableFeignClients annotation. For example, in your case it should look like:

@SpringBootApplication
@EnableFeignClients(clients = {CurrencyConversionServiceProxy.class})
public class CurrencyConversionServiceApplication
Pickford answered 24/9, 2020 at 16:46 Comment(1)
if your clients are not in the same package structure as your Application, then you need to specify the clients as suggested by Elizaveta, otherwise, it won't find the clients. Another option is to provide packages to scan in the basePackages attribute of @EnableFeignClients annotation.Granite
Z
7

You need to specify path for the FeignClients as @EnableFeignClients doesn't use component scan internally to identify the FeignClients within the application

soln->

@EnableFeignClients("com.in28minutes.microservices.currencyconversionservice")

Try the updated code as mentioned below

@SpringBootApplication
@EnableFeignClients("com.in28minutes.microservices.currencyconversionservice")
public class CurrencyConversionServiceApplication {

  public static void main(String[] args) {
     SpringApplication.run(CurrencyConversionServiceApplication.class, args);
  }
}
Zerline answered 31/5, 2021 at 15:34 Comment(1)
Great! This worked for me too! just specify the basepackages in annotation @EnableFeignClients like you did! But still, I don't know why some other modules which depending on the same feign clients could work without such configuration....Contaminate
M
1

I changed Spring Boot version from 2.7 to 3 and got this error. Adding

@ImportAutoConfiguration({FeignAutoConfiguration.class})

to my feign config class (or to main class) solved the issue

Marshamarshal answered 22/8, 2023 at 9:53 Comment(0)
B
0

The issue was with the below dependency being added while the server was running. Hence, restarting the server manually after any change in the pom.xml should fix the issue.

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
Bhagavadgita answered 30/7, 2020 at 6:41 Comment(0)
W
0

With me an update of the Spring Cloud Dependency helped. In general, it is important to keep the Spring Boot Dependency and the Spring Cloud Dependency up to date.

https://spring.io/projects/spring-cloud

Wallache answered 6/8, 2020 at 10:4 Comment(0)
F
0

You can use this dependency instead of only open-config.

<!-- add by WanChengHe 20190507 Increase eureka dependencies -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<!-- Add a feign dependency for inter-service calls add by WanChengHe 20190508 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Furriery answered 17/9, 2020 at 15:0 Comment(0)
S
0

For Hoxton.SR1

Add FeignClient's implementation class FeignClientsConfiguration.class to the annotation.

@FeignClient(name="currency-exchange-service", url="localhost:8000",configuration=FeignClientsConfiguration.class)
Schaab answered 13/12, 2020 at 16:14 Comment(0)
R
0

I just did a maven update and clean install after which it started running correctly.

Rale answered 3/4, 2021 at 19:7 Comment(0)
B
0

Faced a similar problem which was caused by the auto configuration not being found. The clients configurations were defined in their own module that was imported by the spring boot app. The solution was to add resources/META-INF/spring.factories file which had: org.springframework.boot.autoconfigure.EnableAutoConfiguration=<path-to-@Configuration @EnableFeignClients>

Bloodworth answered 14/4, 2021 at 7:4 Comment(0)
E
0

If you are using a parent project, be sure to remove from the parent POM

<plugins>
   <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
</plugins>  
Ezara answered 22/4, 2021 at 15:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.