Quick revision
The short answer
We define one route with an id, the uri lb://orders, a Path=/api/orders/** predicate, and two filters: StripPrefix=1, which removes /api, and AddRequestHeader=X-Source, gateway. The route can be written in YAML or in a RouteLocator bean built with RouteLocatorBuilder, and lb:// makes Spring Cloud LoadBalancer pick an orders instance from service discovery. Recent Spring Cloud releases split the gateway into WebFlux and MVC server modules and moved the property prefix, so we check the version before copying the YAML.
What to remember
In this answer, we built a route with an id, the uri lb://orders, a Path predicate and the StripPrefix and AddRequestHeader filters, in YAML and in a RouteLocator bean. We saw that lb:// lets Spring Cloud LoadBalancer pick an instance from discovery, that recent releases moved the gateway's modules and property prefix, and that an integration test against a stub backend proves the route does what we expect.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We define one route with an id, the uri lb://orders, a Path=/api/orders/** predicate, and two filters: StripPrefix=1, which removes /api, and AddRequestHeader=X-Source, gateway. The route can be written in YAML or in a RouteLocator bean built with RouteLocatorBuilder, and lb:// makes Spring Cloud LoadBalancer pick an orders instance from service discovery. Recent Spring Cloud releases split the gateway into WebFlux and MVC server modules and moved the property prefix, so we check the version before copying the YAML.
Why this question is asked
Routing is the first thing anyone does with Spring Cloud Gateway, and the small details decide whether a request arrives at the right place: which part of the path the backend sees, which headers it gets, and how the gateway finds an instance. Interviewers give this as a coding task because it shows whether we understand predicates and filters rather than just pasting configuration. In this answer, we will recap how a route is built, write it in YAML and in Java, trace one request through it, explain lb://, and finish with an integration test against a stub backend.
1A Quick Recap of Routes, Predicates and Filters
Spring Cloud Gateway is a reverse proxy, a server that receives requests from clients and forwards them to backend services, built on Spring. Its unit of configuration is the route. A route has an id, a destination uri, one or more predicates and a list of filters.
A predicate is a condition on the incoming request, such as its path, host, method or a header. When all predicates of a route match, the gateway uses that route. A filter then changes the request on its way to the backend, or the response on its way back: rewriting the path, adding headers, applying rate limits or retries. Filters run in order, so their position in the list matters.
2Writing the Route in YAML
Here is the route for the question. The gateway in this example is the WebFlux (reactive) variant, the one most projects use.
Notice the version note. In the 2025.0 release train the gateway was reorganised into server modules for WebFlux and for Spring MVC, with new starter names and a new property prefix; the old prefix still works for a while with a deprecation warning. The MVC variant uses its own prefix under spring.cloud.gateway.server.webmvc. Because these names have moved, we check the release notes of the version in our build before copying configuration from an older blog post or project.
StripPrefix=1 removes one path segment from the front, so /api/orders/42 becomes /orders/42. The AddRequestHeader shortcut takes a name and a value separated by a comma, and the space after the comma is trimmed.
3The Same Route in Java
Routes can also be defined in code with RouteLocatorBuilder. This is useful when routes need logic, such as values computed from other beans, and it gives us compile-time checking of the filter names.
8 messages in order. Play them, or step through them one at a time.
Both styles produce the same route, and we can mix them, but a team should pick one as its default so people know where to look.
4What lb:// Means
A plain uri such as http://orders:8080 sends every request to one address. The lb scheme instead names a logical service. When the gateway sees lb://orders, its load balancer filter asks Spring Cloud LoadBalancer for an instance of the service called orders. LoadBalancer gets the list of instances from service discovery, for example Eureka, Consul or Kubernetes, and picks one, round robin by default. The gateway then rewrites the uri to that instance's real host and port.
So the route keeps working when instances come and go: discovery updates the list, and the next request is spread across the current instances. For lb:// to work, we need a discovery client on the classpath, or a fixed list of instances in configuration, which is exactly what we use in tests.
5Testing the Route Against a Stub Backend
Routes are configuration, and configuration deserves tests. An integration test starts the gateway on a random port, points the orders service at a stub server such as WireMock through the simple discovery client, sends a request, and checks what the stub received.
The test proves the three things the question asks for: the path predicate matched, the prefix was stripped, and the header arrived. WireMockPort.get() stands for however our stub library exposes its port; the library's annotations and helpers differ between versions, so we follow its own documentation.
6Follow-up questions
What is the difference between StripPrefix and RewritePath?
StripPrefix removes a number of leading path segments, which is simple and enough here. RewritePath takes a regular expression and a replacement, for example RewritePath=/api/(?<rest>.*), /${rest}, and is the choice when the new path is not just the old one with segments removed.
Where would we add the header for all routes?
In default-filters, a list under the gateway properties that applies to every route, or in a global filter bean. That keeps common headers in one place instead of repeating them on each route.
Should the gateway set X-Source if clients can send it too?
If backends trust the header, the gateway should first remove any client-supplied copy, for example with RemoveRequestHeader before adding its own, or use SetRequestHeader, which replaces the value. Otherwise a client can fake it.
7Conclusion
In this answer, we built a route with an id, the uri lb://orders, a Path predicate and the StripPrefix and AddRequestHeader filters, in YAML and in a RouteLocator bean. We saw that lb:// lets Spring Cloud LoadBalancer pick an instance from discovery, that recent releases moved the gateway's modules and property prefix, and that an integration test against a stub backend proves the route does what we expect.
Open with
I would define a route with the id orders, the uri lb://orders, the predicate Path=/api/orders/**, and two filters: StripPrefix=1, so /api/orders/42 reaches the backend as /orders/42, and AddRequestHeader=X-Source, gateway.
Then, if they want more
In YAML that sits under the gateway routes property; in the current WebFlux server module the prefix is spring.cloud.gateway.server.webflux.routes, while older releases used spring.cloud.gateway.routes, so I would check the version, since the 2025.0 train split the gateway into WebFlux and MVC server starters.
The same route can be built in Java with a RouteLocator bean and RouteLocatorBuilder, path, then filters with stripPrefix and addRequestHeader, then uri.
The lb scheme means the gateway does not call a fixed host: Spring Cloud LoadBalancer looks up instances of orders from service discovery and picks one, round robin by default, so instances can scale up and down without route changes. If backends rely on X-Source, I would also remove any copy the client sends, or use SetRequestHeader.
And I would test it: a SpringBootTest on a random port, the simple discovery client pointing orders at a WireMock stub, a WebTestClient call to /api/orders/42, and a verification that the stub received /orders/42 with the X-Source header.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Cloud documentation, Resilience4j documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Configure a Spring Cloud Gateway route that sends /api/orders/** to the orders service...": https://scrivio-interview-library.netlify.app/spring-boot/spring-cloud/f7e7b76d51/