Spring Cloud Gateway - Unable to find GatewayFilterFactory with name [Filter_Name]

9.5k views Asked by At

I have a spring cloud gateway application. I am trying to setup a gateway filter. The Spring Boot version is 2.3.4.RELEASE Here are the dependencies:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation platform(SpringBootPlugin.BOM_COORDINATES)
    implementation platform('org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR8')
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'

}

Here is the configutraion for the gateway client

server:
  port: 8081
spring:
  cloud:
    gateway:
      routes:
        - id: onboard_redirect
          uri: http://localhost:8080/api/v1/onboard
          predicates:
            - Path=/api/v1/onboard
          filters:
            - name: MyLogging
              args:
                baseMessage: My Custom Message
                preLogger: true
                postLogger: true

Here is my filter class:

@Component
public class MyLoggingGatewayFilterFactory extends AbstractGatewayFilterFactory<MyLoggingGatewayFilterFactory.Config> {

    final Logger logger =
            LoggerFactory.getLogger(MyLoggingGatewayFilterFactory.class);

    public MyLoggingGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            // Pre-processing
            if (config.preLogger) {
                logger.info("Pre GatewayFilter logging: "
                        + config.baseMessage);
            }
            return chain.filter(exchange)
                    .then(Mono.fromRunnable(() -> {
                        // Post-processing
                        if (config.postLogger) {
                            logger.info("Post GatewayFilter logging: "
                                    + config.baseMessage);
                        }
                    }));
        };
    }

    public static class Config {
        public String baseMessage;
        public boolean preLogger;
        public boolean postLogger;
    }
}

Everything works without configuring the filter but when I configure the filter I get follwing error:

reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name MyLogging
Caused by: java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name MyLogging

what I am doing wrong here?

5

There are 5 answers

1
Corina Dumitru On

Move your MyLoggingGatewayFilterFactory.java file into the application root, near YourProjApplication.java. It should work after.

2
Rafael Alexandre On

The filter class is MyLoggingGatewayFilterFactory, not MyLogging as you set in your properties.

Try to the following modification in your application.yml file:

filters:
    - name: MyLoggingGatewayFilterFactory
0
Jay Patel On

Add this dependency in the application.properties file.

<dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-circuitbreaker-reactor- 
     resilience4j</artifactId>
</dependency>
0
dule On

Double check your Spring component scan is picking up your @Component annotation for your particular filter class. By default, Spring will only pick up those under the package of your @SpringBootApplication, so if your filter is under a separate base package, you may need to explicitly tell the component scan to include that package.

e.g.,

@SpringBootApplication
@ComponentScan({"com.myapp", "com.myfilters"})
public class MyApp {
0
Vamsi Jayavarapu On

As per spring documentation @Component is not required for custom filter class.

https://cloud.spring.io/spring-cloud-gateway/reference/html/#writing-custom-gatewayfilter-factories