0

The following example is given in the documenation:

spring:
  cloud:
    gateway:
      routes:
      - id: rewritepath_route
        uri: http://example.org
        predicates:
        - Path=/foo/**
        filters:
        - RewritePath=/foo/(?<segment>.*), /$\{segment}

I have a lot of services, so instead of hard-coding foo in the RewritePath filter, I'd simply like to drop that part dynamically.

I came up with this but it's not working

spring:
  cloud:
    gateway:
      default-filters:
      - RewritePath=/(?<base>.*)/(?<segment>.*), /$\{segment}
      routes:
      - id: rewritepath_route
        uri: http://example.org
        predicates:
        - Path=/foo/**

How does the correct reg exp syntax look like?

Balázs Németh
  • 6,222
  • 9
  • 45
  • 60

1 Answers1

1

I added a ? operator after <base>.* to match only the first part of the path. Got it from here

spring:
  cloud:
    gateway:
      default-filters:
      - RewritePath=/(?<base>.*?)/(?<segment>.*), /$\{segment}
      routes:
      - id: rewritepath_route
        uri: http://example.org
        predicates:
        - Path=/foo/**

Also I didn't realize that it's just plain java regex, thanks @spencergibb.

(Looking at the source code it's not magic at all, if someone is stuck with a similar problem I encourage to do the same. It's easily readable code which does what it promises to do.)

Balázs Németh
  • 6,222
  • 9
  • 45
  • 60