Home > OS >  Feign Client being ignored during component scan
Feign Client being ignored during component scan

Time:04-12

I am creating a feign client to hit an external service. I added the following dependencies to my 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>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>in.njari</groupId>
    <artifactId>util</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>util</name>
    <description>Utilities for API Dev</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>2.6.6</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>Greenwich.SR3</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>

The two additions here are spring-cloud-starter-openfeign and spring-cloud-dependencies

Here is my feign client class :

@FeignClient(name = "chat-service", url = "${server.chatservice.url}")
public interface ChatServiceClient {

    @PostMapping("/email/send")
    void sendEmail(@RequestBody Map<String, String> sendEmailMap);
}

Next I'm autowiring this in one of my existing services. However, this leads to an error. What am I missing?

I tried adding @Service to my chatServiceClient, however that simply gives me this warning : 2022-04-11 17:02:07.358 DEBUG 87001 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Ignored because not a concrete top-level class: file [/Users/rajdeep/X/util/target/classes/in/njari/util/src/client/ChatServiceClient.class]

I think @FeignClient should be enough to register this as a bean, however it's not. What else do I need?

2022-04-11 16:50:41.917  INFO 86938 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$247290de] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.5.RELEASE)

2022-04-11 16:50:42.110  INFO 86938 --- [           main] in.njari.util.UtilApplication            : No active profile set, falling back to default profiles: default
2022-04-11 16:50:42.236 DEBUG 86938 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Identified candidate component class: file [/Users/rajdeep/X/util/target/classes/in/njari/util/src/ExceptionReportingAdvice.class]
2022-04-11 16:50:42.268 DEBUG 86938 --- [           main] o.s.c.a.ClassPathBeanDefinitionScanner   : Identified candidate component class: file [/Users/rajdeep/X/util/target/classes/in/njari/util/src/controller/UtilController.class]
2022-04-11 16:50:42.978  INFO 86938 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=9ddeaaa5-b9f2-314c-82de-bbd43b8d74ad
2022-04-11 16:50:43.129  INFO 86938 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$247290de] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-04-11 16:50:43.732  INFO 86938 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-04-11 16:50:43.753  INFO 86938 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-04-11 16:50:43.753  INFO 86938 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2022-04-11 16:50:43.887  INFO 86938 --- [           main] o.a.c.c.C.[.[localhost].[/util-service]  : Initializing Spring embedded WebApplicationContext
2022-04-11 16:50:43.888  INFO 86938 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1722 ms
2022-04-11 16:50:43.930  WARN 86938 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'exceptionReportingAdvice': Unsatisfied dependency expressed through field 'chatServiceClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'in.njari.util.src.client.ChatServiceClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2022-04-11 16:50:43.932  INFO 86938 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-04-11 16:50:43.948  INFO 86938 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-04-11 16:50:43.948 DEBUG 86938 --- [           main] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@28cda624, started on Mon Apr 11 16:50:41 IST 2022
2022-04-11 16:50:44.091 ERROR 86938 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field chatServiceClient in in.njari.util.src.ExceptionReportingAdvice required a bean of type 'in.njari.util.src.client.ChatServiceClient' that could not be found.

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


Action:

Consider defining a bean of type 'in.njari.util.src.client.ChatServiceClient' in your configuration.


Process finished with exit code 1

CodePudding user response:

try @EnableFeignClients in your main class:

@SpringBootApplication
@EnableFeignClients
public class ExampleApplication {

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

see more: https://www.baeldung.com/spring-cloud-openfeign

  • Related