Skip to content
Advertisement

Spring Cloud Gateway Route URL pattern configuration

I’m have implemented Spring Cloud Gateway with Eureka discovery service, everything works fine but I have seen something that I don’t know how to deal with when I write the URL and if I don’t put a / at the end of the URL the gateway redirects to the app directly using its actual URL (registered in Eureka).

For example:

Is there a configuration to avoid the first situation?

My configuration is the following:

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: bar-service
          uri: lb://BAR-SERVICE/
          predicates:
            - Path=/bar/**
        - id: other-service
          uri: lb://OTHER-SERVICE/
          predicates:
            - Path=/OTHER/**

Additional information:

  • I have a controller in every app that has ‘/’ as an entry point (home page)
  • I can use java configuration instead if necessary

Any advice will be appreciated! Cheers!

Advertisement

Answer

You should use RewritePath in gateway configuration. The below is sample and hope it helpful to you.

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: bar-service
          uri: lb://BAR-SERVICE/
          predicates:
            - Path=/bar/**
          filters:
            - RewritePath=/bar(?<segment>.*), /${segment}

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement