-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Support dynamic paths in route URI using SetRequestUri filter #3761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Stepan Mikhailiuk <[email protected]>
@spencergibb, sorry, I didn't figure out how to request review, expected automatic PR assign. |
String url = getUri(exchange, config); | ||
URI uri = URI.create(url); | ||
if (!uri.isAbsolute()) { | ||
throw new IllegalArgumentException("URI is not absolute"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why throw then catch this exception only to return Optional.ofNullable(null)
? Couldn't you just call log.info
and then return here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryanjbaxter , yeah, you are right! fixed, thank you!
Signed-off-by: Stepan Mikhailiuk <[email protected]>
Signed-off-by: Stepan Mikhailiuk <[email protected]>
Can you take a look at the CI build? |
Background
Spring Cloud Gateway does not support dynamic path segments directly in the uri field of a route. This limitation makes it challenging to forward requests to upstream services whose hostnames or paths depend on request parameters (e.g. a path variable like {appId}).
What’s Changed
This PR introduces an update to the routing configuration that leverages the SetRequestUri GatewayFilter factory to dynamically construct the request URI based on path variables. By using URI templates supported by Spring Framework, we can now rewrite the request URI at runtime with values extracted from the incoming request path.
Example Configuration
For a request path of
/red-application/blue
, this sets the uri tohttp://red-application.internal.com
before making the downstream request and the final url, including path is going to behttp://red-application.indernal.com/red-application/blue
Additional notes
This filter is similar to
RequestHeaderToRequestUriGatewayFilterFactory but more generic