spring-cloud-openfeign: Could not autowire @FeignClient
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.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 23 (9 by maintainers)
add(require = false)after your @Autowired, that can solve the problem ,But it didn’t address the root cause
The same problem. And you can use @EnableFeignClients(basePackages = “com.xxx.xxx”), for scan feign client
Make sure your import is correct: import org.springframework.cloud.openfeign.FeignClient;
NOT: import org.springframework.cloud.netflix.feign.FeignClient;
Thank you, here working.