Could not autowire @FeignClient

See original GitHub issue

I try to autowire feign client into the service, but the error is Could not autowire. No beans of ‘OperatorClient’ type found. Spring cloud version Finchley.SR2 Spring boot version 2.0.6.RELEASE

That is pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.spboot</groupId>
    <artifactId>cloudexample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloudexample</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
        <repository>
            <id>bintray-kptfh-feign-reactive</id>
            <name>bintray</name>
            <url>https://dl.bintray.com/kptfh/feign-reactive</url>
        </repository>
    </repositories>

</project>

That is main class:

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class CloudExampleApplication {

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

}

FeignClient:

@FeignClient(name = "cloud-example")
public interface OperatorClient {

    @RequestMapping(value = "/cloud-example/users", method = RequestMethod.GET)
    String getUsers();

}

Controller:

@RestController
public class OperatorController {

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public String getUsers() {
        return "Man";
    }

}

Service:

@Data
@Service
public class CloudService {

    @Autowired
    private OperatorClient client;

    public String getName() {
        return client.getUsers();
    }
}

Another Controller:

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private CloudService service;

    @GetMapping("/hello")
    public String getUser() {
        return "Hello " + service.getName() + "!";
    }
}

So, in CloudService the OperatorClient isn’t autowiring.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:23 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
LiYeLincommented, Mar 23, 2020

add(require = false)after your @Autowired, that can solve the problem ,But it didn’t address the root cause

3reactions
apostrophecommented, Aug 18, 2019

Make sure your import is correct: import org.springframework.cloud.openfeign.FeignClient;

NOT: import org.springframework.cloud.netflix.feign.FeignClient;

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Injecting FeignClient from another Project - Stack Overflow
I am having trouble auto wiring a feign client from another project. It appears that the implementation of the feign client is not...
Read more >
Spring: support meta-annotations w/ @EnableFeignClients
Inspection "Could not autowire. No beans of 'SomeFeignClient' type found" reports when trying to autowire a class annotated with @FeignClient, even when it...
Read more >
[Solved]-Could not autowire FeignClient in service-Springboot
I think you have to inject the Service in your Controller . You created your Service as a normal Java-class so the MyClient...
Read more >
7. Declarative REST Client: Feign - Spring Cloud
This will cause @Autowired to not work because there isn't exactly one bean, or one marked as primary. To work around this, Spring...
Read more >
Configure Feign Client in Spring Boot - Coding N Concepts
Feign vs RestTemplate · Do not need to write implementation classes to call other services, just provide specification as an Interface · Client ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found