Skip to content

Commit

Permalink
Documents registration of custom filters and predicates.
Browse files Browse the repository at this point in the history
This allows them to be used in external configuration.

Fixes gh-3268
  • Loading branch information
spencergibb committed Mar 19, 2024
1 parent 430607c commit f0ad75a
Showing 1 changed file with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,69 @@ class RouteConfiguration {

The above route will add a `X-Response-Id` header to the response. Note the use of the `after()` method, rather than `filter()`.

// TODO: registering for configuration
== How To Register Custom Predicates and Filters for Configuration

To use custom Predicates and Filters in external configuration you need to create a special Supplier class and register it in `META-INF/spring.factories`.

=== Registering Custom Predicates

To register custom predicates you need to implement `PredicateSupplier`. The `PredicateDiscoverer` looks for static methods that return `RequestPredicates` to register.


SampleFilterSupplier.java
[source,java]
----
package com.example;
import org.springframework.cloud.gateway.server.mvc.predicate.PredicateSupplier;
@Configuration
class SamplePredicateSupplier implements PredicateSupplier {
@Override
public Collection<Method> get() {
return Arrays.asList(SampleRequestPredicates.class.getMethods());
}
}
----

You then need to add the class in `META-INF/spring.factories`.

.META-INF/spring.factories
[source]
----
org.springframework.cloud.gateway.server.mvc.predicate.PredicateSupplier=\
com.example.SamplePredicateSupplier
----

=== Registering Custom Filters

The `SimpleFilterSupplier` allows for easily registering custom filters. The `FilterDiscoverer` looks for static methods that return `HandlerFilterFunction` to register. If you need more flexibility than `SimpleFilterSupplier` you can implement `FilterSupplier` directly.

.SampleFilterSupplier.java
[source,java]
----
package com.example;
import org.springframework.cloud.gateway.server.mvc.filter.SimpleFilterSupplier;
@Configuration
class SampleFilterSupplier extends SimpleFilterSupplier {
public SampleFilterSupplier() {
super(SampleAfterFilterFunctions.class);
}
}
----

You then need to add the class in `META-INF/spring.factories`.

.META-INF/spring.factories
[source]
----
org.springframework.cloud.gateway.server.mvc.filter.FilterSupplier=\
com.example.SampleFilterSupplier
----

// TODO: advanced topics such as attributes, beans and more

0 comments on commit f0ad75a

Please sign in to comment.