Skip to content

Support dynamic paths in route URI using SetRequestUri filter#3761

Merged
ryanjbaxter merged 6 commits intospring-cloud:mainfrom
stepancar:setrequesturi
May 22, 2025
Merged

Support dynamic paths in route URI using SetRequestUri filter#3761
ryanjbaxter merged 6 commits intospring-cloud:mainfrom
stepancar:setrequesturi

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

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

Comments