0

I have the following routes mapped via @RequestMapping (GET) in a Spring Boot Rest Controller

/base/{baseName}
/base/{baseName}/next/{nextName}

I also have the following wildcard routes

1. /base/{baseName}/**
2. /base/{baseName}/next/{nextName}/*

Which work for the following cases

GET /base/baseName
GET /base/baseName/foo                  # Hits 1
GET /base/baseName/foo/bar/etc          # Hits 1

GET /base/baseName/next/nextName
GET /base/baseName/next/nextName/foo    # Hits 2

The only situation that doesn't work is

GET /base/baseName/next/nextName/foo/bar... # Hits 1

as I want it to hit route 2 instead.

It doesn't work because the matching precedence in Spring Boot dictates that wildcard routes 1 and 2 have the same values (1 point for every wildcard, 1 point for every template).

Since route 2 can't match unlimited directories, it doesn't work. If I were to add an additional wildcard, route 1 would always take precedence.

Is there any way to change the precedence rules, or restructure my mappings?

mrybak3
  • 385
  • 1
  • 3
  • 14

1 Answers1

1

Those are ant-style regular expression where /* means 1 and exactly one level and /** means multiple levels. So it will indeed match the first one as it simply doesn't match the second one. You will need to add /** to the second one as well. Or if you want 1 or 2 directories add an additional path mapping (value takes a String[]!).

Depending on your needs use /** or second mapping.

@RequestMapping(value="/base/{baseName}/next/{nextName}/**")

or a second mapping (if you want to go only 1 or 2 levels deep).

@RequestMapping(value= { 
    "/base/{baseName}/next/{nextName}/*", 
    "/base/{baseName}/next/{nextName}/*/*" })
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • I want to handle the case where the second mapping does not override the first one and accepts unlimited levels, so neither of these work – mrybak3 May 23 '19 at 07:02
  • It should, have you actually tested this... As the `/next` is in there it will match as it takes precedence. Spring detects the best matching one. – M. Deinum May 23 '19 at 07:04
  • Yes i have, it completely overrides the logic of the second mapping and only the first one gets called, since the precedence is based on the "score" assigned to it via the ant path matcher before the raw /next matching – mrybak3 May 23 '19 at 07:10