Search code examples
spring-cloud-gateway

Apply filters to different paths of the same URI in Spring Cloud Gateway


I have filters defined like this:

- id: Some route
      uri: http://localhost
      filters:
        - name: Filter1
        - name: Filter2
      predicates:
        - Path=/api/**

However, I would like to apply Filter2 only to /api/somepath/** and keep Filter 1 as it currently is. I've tried to put Filter2 in it's own route, but then it's not picked up anymore:

    - id: Some route1
      uri: http://localhost
      filters:
        - name: Filter1
      predicates:
        - Path=/api/**

    - id: Some route2
      uri: http://localhost
      filters:
        - name: Filter2
      predicates:
        - Path=/api/somepath/**

How can I do this?


Solution

  • Switch the order of route definitions. Define Some route2 first and then Some route1 like this

    - id: Some route2
      uri: http://localhost
      filters:
       - name: Filter2
      predicates:
       - Path=/api/somepath/**
    
    - id: Some route1
      uri: http://localhost
      filters:
       - name: Filter1
      predicates:
       - Path=/api/**
    

    In your configuration, path is always matching with Some route1 and that's why first configuration is picked