Search code examples
springspring-cloud-gateway

Spring Cloud Gateway: why route uri property ignored?


I have only this route conf

spring.cloud.gateway.routes[0].id=x-service
spring.cloud.gateway.routes[0].uri=http://localhost:5555/x-service/v1/private/files
spring.cloud.gateway.routes[0].predicates[0]=Path=/v1/private/files

but app redirects to /v1/private/files.

How to fix this?


Solution

  • AFAIK spring-cloud-gateway Route builder takes into account only hostname and port passed to UriSpec.uri(String uri), so only http://localhost:5555 matters at this point.

    So you need to rewrite your path, using RewritePath gateway filter, like this:

    spring.cloud.gateway.routes[0].filters[0]=RewritePath=/v1/private/files, /x-service/v1/private/files
    

    First argument is the original path, second - a replacer.
    RegEx also can be used there.

    Take a look at spring-cloud-gateway reference docs for details.
    Also I suggest reading this article.