2

I have this RequestMapping:

@RequestMapping(value = "/route/to-{destination}-from-{departure}.html", method = {RequestMethod.GET, RequestMethod.HEAD})

And I would like to add that RequestMapping:

@RequestMapping(value = "/route/to-{destination}.html", method = {RequestMethod.GET, RequestMethod.HEAD})

So it can serve all the "without departure" routes. That, however, creates a conflict as a "/route/to-destination-from-departure" url actually match this second RequestMapping as well... Fair enough so my solution was to specify a regex:

@RequestMapping(value = "/route/to-{destination:^((?!-from-).)+}.html", method = {RequestMethod.GET, RequestMethod.HEAD})

So that RequestMapping will not match if "destination" contains "-from-".

And... it does not work! The url "/route/to-barcelona-from-paris.html" is succesfully served by the first RequestMapping but the url "/route/to-barcelona.html" is not served at all... What I am missing?

Notes: I would like not to use a java solution such as having a single "/route/to-{destination}" RequestMapping then checking if "destination" contains "-from-".. :) Also, I can't alter those routes because of SEO...

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
AddJ
  • 63
  • 7
  • Try `{destination:(?!.*-from-).+}.html` or `{destination:(?!.*-from-)[^\/]+}.html`. – Wiktor Stribiżew Aug 24 '18 at 11:52
  • @WiktorStribiżew "{destination:(?!.*-from-).+}" worked ! You made my day, thank you very much! ("{destination:(?!.*-from-)[^\/]+}" did not) As a reward, here is a strip about you haha: http://www.commitstrip.com/en/2014/02/24/coder-on-the-verge-of-extinction/ – AddJ Aug 24 '18 at 14:06

2 Answers2

6

You may use

"/route/to-{destination:(?!.*-from-).+}.html"

The ^ anchor would search for the start of the string and will fail any match here.

The (?!.*-from-) negative lookahead will fail any input that contains -from- after any 0+ chars other than line break chars.

The .+ pattern will consume all 1 or more chars other than line break chars to the end of the line.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

try using this :->

{destination:^(?!from)+}

or

{destination:^((?!-from-).)+}

oks
  • 21
  • 1
  • 5
  • Tried both but didn't worked! "{destination:(?!.*-from-).+}.html" from Wiktor Stribiżew worked though so there is our solution! Thanks for helping! – AddJ Aug 24 '18 at 14:09