Search code examples
spring-bootspring-cloudopenfeign

EnableFeignClients and FeignClient annotation is not recognized in application class file


I am using spring boot starter parent version 2.6.3(latest version). I am using spring cloud version 2021.0.0 . I am using openfeign dependency in maven pom file. But, my import statements in the class are not recognizable. Also, I am getting red tick in pom.xml. If I comment out openfeign dependency , red tick disappears from pom.xml . I tried to check openfeign jar files content, it does not have FeignClient and EnableFeignClients class files. So, it does make sense to get error while importing in my application class file.

What should I do to fix the import statements? I would like to have working version of dependency. I saw earlier versions tutorial but however I am looking forward to work with current version. I tried to put older version like spring boot starter parent 2.4.8 with spring cloud dependencies 2020.0.3 but I am getting same error for openfeign class files.

Here is my code.


My application class file.

import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;

@Configuration
@EnableFeignClients
@EnableDiscoveryClient
public class HelloWorldClientConfig {
    
        @Autowired
        private TheClient theClient;

        @FeignClient(name = "HelloWorld")
        interface TheClient {    
            @RequestMapping(path = "/helloworld", method = RequestMethod.GET)
            @ResponseBody
            String helloWorld();
        }
        public String HelloWorld() {
            return theClient.helloWorld();
        }
}

Here is my pom.xml file.

<?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 https://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.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.travelport.ts</groupId>
    <artifactId>HelloWorldConsumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>HelloWorldConsumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <!--  spring-cloud.version>Hoxton.SR4</spring-cloud.version-->
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <!--  version>2.2.6.RELEASE</version-->
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>     
        </dependency>

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

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

</project>
    

Solution

  • Try adding another dependency in your pom.

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