springspring-bootintellij-ideaopenfeignjava-18

Cannot autowire open feign client


I have the following open feign client:

package com.example.feignclientch11.clients;

import com.example.feignclientch11.entities.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;

@FeignClient(name = "payments",
            url = "${name.service.url}")
public interface PaymentsProxy {

    @PostMapping("/payment")
    Payment createPayment(@RequestHeader String requestId, @RequestBody Payment payment);
}

and the configuration class to enable open feign clients:

package com.example.feignclientch11;

import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableFeignClients(basePackages = "com.example.feignclientch11.clients")
public class ProjectConfig {

}


and then I try to autowire it in the controller:

@RestController
public class PaymentsController {

    private final PaymentsProxy paymentsProxy;

    public PaymentsController(PaymentsProxy paymentsProxy) {
        this.paymentsProxy = paymentsProxy;
    }

    @GetMapping("/payment")
    public Payment createPayment(@RequestBody Payment payment) {
        String requestId = UUID.randomUUID().toString();
        return paymentsProxy.createPayment(requestId, payment);
    }
}

and this is the main class:

package com.example.feignclientch11;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Feignclientch11Application {

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

}

but intellij complains Could not autowire. No beans of 'PaymentsProxy' type found. that it is not able to autowire the bean.

This is the runtime log:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.3.2)

2024-07-27T18:23:34.701+03:00  INFO 7074 --- [feignclientch11] [  restartedMain] c.e.f.Feignclientch11Application         : Starting Feignclientch11Application using Java 18.0.2 with PID 7074 (/Users/cristiandanila/Desktop/pers-workspace/snippets/book-snippets/spring-start-here/ch11/feignclientch11/target/classes started by cristiandanila in /Users/cristiandanila/Desktop/pers-workspace/snippets/book-snippets/spring-start-here/ch11/feignclientch11)
2024-07-27T18:23:34.703+03:00  INFO 7074 --- [feignclientch11] [  restartedMain] c.e.f.Feignclientch11Application         : No active profile set, falling back to 1 default profile: "default"
2024-07-27T18:23:34.740+03:00  INFO 7074 --- [feignclientch11] [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2024-07-27T18:23:35.114+03:00  INFO 7074 --- [feignclientch11] [  restartedMain] o.s.cloud.context.scope.GenericScope     : BeanFactory id=7b4988c0-43d4-309b-bebe-377053bec13d
2024-07-27T18:23:35.249+03:00  INFO 7074 --- [feignclientch11] [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2024-07-27T18:23:35.397+03:00  INFO 7074 --- [feignclientch11] [  restartedMain] c.e.f.Feignclientch11Application         : Started Feignclientch11Application in 0.958 seconds (process running for 6.437)

Process finished with exit code 0

This is the pom.xml:

<?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>3.3.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>feignclientch11</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>feignclientch11</name>
    <description>feignclientch11</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2023.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <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.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>
    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Not sure why it behaves like this.


Solution

  • It is working now after removing spring-dev-tools and adding spring-starter-web dependencies to pom.xml.

    <?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>3.3.2</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>feignclientch11</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>feignclientch11</name>
        <description>feignclientch11</description>
        <url/>
        <licenses>
            <license/>
        </licenses>
        <developers>
            <developer/>
        </developers>
        <scm>
            <connection/>
            <developerConnection/>
            <tag/>
            <url/>
        </scm>
        <properties>
            <java.version>17</java.version>
            <spring-cloud.version>202thank you! I3.0.3</spring-cloud.version>
        </properties>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
                <version>4.1.3</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</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>
    <!--    <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>-->
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>