Built-in GatewayFilters You Should Know
Explore the most useful out-of-the-box GatewayFilter factories so you can solve common tasks without writing custom code.
Built-in GatewayFilters You Should Know is a free API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Filters You Get for Free
Before writing custom filters, know that Spring Cloud Gateway ships with dozens of ready-made GatewayFilter factories. You attach them per route in configuration.
This lesson tours the ones you will reach for most often.
AddRequestHeader
AddRequestHeader injects a header into every request before it reaches the backend, useful for tagging traffic from the gateway.
filters:
- AddRequestHeader=X-Source, gatewayAddResponseHeader
Likewise, AddResponseHeader adds a header to the response sent back to the client, for example a marker showing which gateway handled it.
filters:
- AddResponseHeader=X-Handled-By, edge-gatewayRewritePath
RewritePath uses a regex to transform the incoming path before forwarding. Strip a public prefix while keeping the rest.
filters:
- RewritePath=/api/(?<seg>.*), /$\{seg}StripPrefix
When you just need to drop leading path segments, StripPrefix is simpler than a rewrite. StripPrefix=1 removes the first segment.
filters:
- StripPrefix=1
# /service/users -> /usersPrefixPath
The reverse of stripping, PrefixPath prepends a segment to the path sent upstream.
filters:
- PrefixPath=/internal
# /users -> /internal/usersSetStatus
SetStatus forces the HTTP status code of the response, handy for maintenance routes or deprecation notices.
filters:
- SetStatus=410RedirectTo
RedirectTo returns a redirect response with a status and target URL instead of proxying.
filters:
- RedirectTo=302, https://new.example.comRemoveRequestHeader
Strip a header before it reaches the backend with RemoveRequestHeader, for example to drop a client-supplied header you do not trust.
filters:
- RemoveRequestHeader=CookieA Full Route Example
Filters stack in declared order. Here a route strips a prefix, tags the request, and marks the response.
routes:
- id: users
uri: http://users-svc:8080
predicates:
- Path=/api/users/**
filters:
- StripPrefix=2
- AddRequestHeader=X-Source, gateway
- AddResponseHeader=X-Handled-By, edgeDefault Filters
Use default-filters to apply a filter to every route at once, such as adding a correlation header everywhere.
spring:
cloud:
gateway:
default-filters:
- AddResponseHeader=X-Gateway, trueQuick Check
You want to turn /api/users into /users by dropping the first path segment. Which filter is the simplest fit?
Recap
You now have a toolbox of built-in filters:
- Header filters: Add/Remove request and response headers
- Path filters: StripPrefix, PrefixPath, RewritePath
- Response control: SetStatus, RedirectTo
default-filtersapply globally
Reach for these before writing custom code, then build custom filters only when nothing fits.
Frequently asked questions
Is the “Built-in GatewayFilters You Should Know” lesson free?
Yes — the full text of “Built-in GatewayFilters You Should Know” is free to read here on the web, and the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) course, upgrade to CoddyKit PRO.
What will I learn in “Built-in GatewayFilters You Should Know”?
Explore the most useful out-of-the-box GatewayFilter factories so you can solve common tasks without writing custom code. You practise API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?
No prior experience is required. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Built-in GatewayFilters You Should Know” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson?
Yes. Every API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Global Filters & GatewayFilterFactory
- Custom Request/Response Filters
- Pre/Post Processing with Filters
- Built-in GatewayFilters You Should Know