0

I have a problem with spring and openfeign in which I think you can help me.

I have a pom file as follows:

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

    <groupId>xx.yyy</groupId>
    <artifactId>component</artifactId>
    <version>1.0.0</version>
    <name>component</name>

    <properties>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
        <pact.version>3.6.7</pact.version>
        <spring-cloud.version>Greenwich.SR4</spring-cloud.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
        <relativePath />
    </parent>


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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</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>
......
<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>
......

I have declared in the main class the following configurations:

@SpringBootApplication(scanBasePackages = {"xx.yyy", "xx.yyy.rest.client"}, exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableAsync
@Import({P2OrchestratorApplicationConfig.class})
public class P2OrchestratorApplication {

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

I have a custom feign configuration class:

@Configuration
@EnableFeignClients()
@ImportAutoConfiguration({FeignAutoConfiguration.class})
public class FeignConfig {

    @Bean
    public OkHttpClient client() {
        return new OkHttpClient();
    }
    
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
    
    @Bean
    public Contract feignContract() {
        return new feign.Contract.Default();
    }
}

I have a open feign client as follow:

@FeignClient(name="legacyClient", value = "legacyClient", url = "${uri.microservice.legacy}", configuration = FeignConfig.class)
public interface LegacyClient {
    
    @PatchMapping(value = "/legacy/xxx/cleanLine/{authorizationCode}", produces = { MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<Boolean> cleanLine(@PathVariable("authorizationCode") Long authorizationCode, @RequestParam(required = true) Long lineNumber);
    

}

and finally a Component in which I need to use this client:

@Log4j
@Component("p2ProcessAlgorithm")
public class P2ProcessAlgorithm {

@Autowired
@Qualifier("legacyClient")
private LegacyClient legacyClient;

public final void process(){

Long authorizationCode = 123L;
Long lineNumber = 1L;
Boolean isClean= this.legacClient.cleanLine(authorizationCode, lineNumber);
......
}

But the app give me the next message:

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

Description:

Field legacyClient in xxx.yyy.p2.structure.P2ProcessAlgorithm required a bean of type 'xxx.yyy.rest.client.LegacyClient' that could not be found.

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


Action:

Consider defining a bean of type 'xxx.yyy.rest.client.LegacyClient' in your configuration.

I've tried several configurations but I can't get the openfeign client to be a recognized bean in the P2ProcessAlgorithm class.

Do you have any idea?

Thanks in advance

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Amador
  • 1
  • 1
  • 4
  • maybe try adding package name where feignclient is located in basepackages field of @enablefeignclients; and do mvn install – 9900kf Aug 20 '23 at 13:11

3 Answers3

0

A couple of points that might help:

First off: Why do you use a qualifier to inject the LegacyClient bean into your service? Are there many? In general an interface should be wrapped into proxy and put as a single bean into the Application Context, so in my understanding Qualifier is not required here.

Another concern:

It looks like the @FeignClient annotation is not processed. Therefore the proxy is not created. In order to check this, place a breakpoint into the configuration FeignConfig some of beans / create a no-op constructor and check that it gets called at all (or mayble place log message just to see it works).

You haven't posted the package structure but it's possible that this configuration is not found by the package scanning mechanism and therefore is not created.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
0

first of all thank you for your reply.

I answered your first question, Yeah, it's just a habit. I can eliminate that without problems. There are no more Beans that occupy that same interface. It's not a problem.

Responding to your second comment, you are absolutely right.

Add a builder to the FeignConfig configuration class. Add a log and a breakpoint and don't stop there.

Attached a ss with the package structure, to see if you can help me detect my problem.

project package structure

  • In ms.config package, I have the FeignConfig configuration class
  • In rest.client package, I have all the interfaces Feign Client
  • In the ms package is the main class with the notation SpringBootApplication
  • In ms.p2.structure package is the class that call the feign client.

Thank you very much in advance.

Amador
  • 1
  • 1
  • 4
-1
@SpringBootApplication

@EnableFeignClients

public class Application {
    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }

}
astqx
  • 2,058
  • 1
  • 10
  • 21
Sergey
  • 19