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

Merged
merged 6 commits into from
May 22, 2025

Conversation

stepancar
Copy link
Contributor

@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

@stepancar
Copy link
Contributor Author

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

String url = getUri(exchange, config);
URI uri = URI.create(url);
if (!uri.isAbsolute()) {
throw new IllegalArgumentException("URI is not absolute");
Copy link
Contributor

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?

Copy link
Contributor Author

@stepancar stepancar May 20, 2025

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!

@ryanjbaxter
Copy link
Contributor

Can you take a look at the CI build?

@stepancar
Copy link
Contributor Author

stepancar commented May 22, 2025

@ryanjbaxter , thank you, updated test, AFAIK I can't trigger the CI build myself to check everything correct

@ryanjbaxter ryanjbaxter added this to the 4.3.0 milestone May 22, 2025
@ryanjbaxter ryanjbaxter merged commit 2dfc3f3 into spring-cloud:main May 22, 2025
2 checks passed
@github-project-automation github-project-automation bot moved this to Done in 2025.0.0 May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants