Skip to content

Commit f0ad75a

Browse files
committed
Documents registration of custom filters and predicates.
This allows them to be used in external configuration. Fixes gh-3268
1 parent 430607c commit f0ad75a

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

docs/modules/ROOT/pages/spring-cloud-gateway-server-mvc/writing-custom-predicates-and-filters.adoc

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,69 @@ class RouteConfiguration {
231231

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

234-
// TODO: registering for configuration
234+
== How To Register Custom Predicates and Filters for Configuration
235+
236+
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`.
237+
238+
=== Registering Custom Predicates
239+
240+
To register custom predicates you need to implement `PredicateSupplier`. The `PredicateDiscoverer` looks for static methods that return `RequestPredicates` to register.
241+
242+
243+
SampleFilterSupplier.java
244+
[source,java]
245+
----
246+
package com.example;
247+
248+
import org.springframework.cloud.gateway.server.mvc.predicate.PredicateSupplier;
249+
250+
@Configuration
251+
class SamplePredicateSupplier implements PredicateSupplier {
252+
253+
@Override
254+
public Collection<Method> get() {
255+
return Arrays.asList(SampleRequestPredicates.class.getMethods());
256+
}
257+
258+
}
259+
----
260+
261+
You then need to add the class in `META-INF/spring.factories`.
262+
263+
.META-INF/spring.factories
264+
[source]
265+
----
266+
org.springframework.cloud.gateway.server.mvc.predicate.PredicateSupplier=\
267+
com.example.SamplePredicateSupplier
268+
----
269+
270+
=== Registering Custom Filters
271+
272+
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.
273+
274+
.SampleFilterSupplier.java
275+
[source,java]
276+
----
277+
package com.example;
278+
279+
import org.springframework.cloud.gateway.server.mvc.filter.SimpleFilterSupplier;
280+
281+
@Configuration
282+
class SampleFilterSupplier extends SimpleFilterSupplier {
283+
284+
public SampleFilterSupplier() {
285+
super(SampleAfterFilterFunctions.class);
286+
}
287+
}
288+
----
289+
290+
You then need to add the class in `META-INF/spring.factories`.
291+
292+
.META-INF/spring.factories
293+
[source]
294+
----
295+
org.springframework.cloud.gateway.server.mvc.filter.FilterSupplier=\
296+
com.example.SampleFilterSupplier
297+
----
235298

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

0 commit comments

Comments
 (0)