Search code examples
javaspringspring-bootspring-cloudspring-cloud-gateway

spring gateway: how to dynamically set URI and path based on request headers and path


let's say i have incoming request: host: my-XXX.domain.com and path /YYY/ZZZ and i want to route that request to uri XXX.YYY.internal.domain.com/ZZZ. how can i achieve it?

standard api doesn't seem to allow any kind of pattern extraction

builder.routes()
    .route{ it
            .header(xxx,xxx)
            .path("/*/**")
            .uri("i can't use here anything captured in header or path function")
    }

there is a function which gives me access to request and allow to return any URI

                        .filters{
                            it.changeRequestUri {
                                val service = it.request....
                                Optional.of(URI("http://...."))
                            }
                        }
                        .uri("https://this will be ignored")

but i can't set path there.

is there any existing api to simply achieve it or do i have to write custom filter? how to do it properly?


Solution

  • this is what worked for me

    .filters{
        it.changeRequestUri {
            val (service, environment, newPath) = computeUrlParts(it)
            Optional.of(URI("http://$environment$service.internal.my-company.com/$newPath"))
        }
    }
    .uri("http://postman-echo.com") //ignored