Spring Cloud Gateway doesn't see the slash at the end of the url request

Viewed 222

I have the following configuration:

gateway:
  discovery:
  locator:
    enabled: true
    predicates:
      - name: Path
        args:
          pattern: "'/api/'+serviceId.toLowerCase()+'/**'"
    filters:
      - name: RewritePath
        args:
          regexp: "'/api/' + serviceId.toLowerCase() + '/(?<remaining>.*)'"
          replacement: "'/${remaining}'"

If I send request with ...api/serviceName/ - OK

But if I send a request with ...api/serviceName - it returns 404 NOT FOUND

How can I change the properties so I can use the URL without the slash at the end?

Thanks!

2 Answers

I believe you're mandating the slash in the predicate regex :

pattern: "'/api/'+serviceId.toLowerCase()+'/**'"

try making it conditional (0 or 1 instances of "/**" ) :

pattern: "'/api/'+serviceId.toLowerCase()+'(/**)?'"

The problem was extra slash in the filter:

This:

regexp: "'/api/' + serviceId.toLowerCase() + '/(?<remaining>.*)'"
                replacement: "'/${remaining}'"

Should be replaced by this:

regexp: "'/api/' + serviceId.toLowerCase() + '(?<remaining>.*)'"
                replacement: "'/${remaining}'"
Related