Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

stepancar
Copy link

@stepancar stepancar commented Apr 13, 2025

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

spring:
  cloud:
    gateway:
      routes:
      - id: seturi_route
        uri: no://op
        predicates:
        - Path=/{appId}/**
        filters:
        - SetRequestUri=http://{appId}.internal.com

For a request path of /red-application/blue, this sets the uri to http://red-application.internal.com before making the downstream request and the final url, including path is going to be http://red-application.indernal.com/red-application/blue

Additional notes

This filter is similar to

RequestHeaderToRequestUriGatewayFilterFactory but more generic

Signed-off-by: Stepan Mikhailiuk <istepancar@gmail.com>
@stepancar
Copy link
Author

@spencergibb, sorry, I didn't figure out how to request review, expected automatic PR assign.
Could you look at it? Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants