0

I have the following request mappings in my controllers, but when i hit /united-states/georgia it always goes to the method mapped with {country/city} even though . How to force it to go to the united-states method. Any suggestion would be appreciated.

@RequestMapping(value = "/{country:united-states|canada}/{state}")
public String getCitites(@PathVariable String country,@PathVariable String state){
.....
}

@RequestMapping(value = "/{country}/{city}")
public String getDetailsInCity(@PathVariable String country,@PathVariable String city){
.....
}
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
minion
  • 4,313
  • 2
  • 20
  • 35
  • try to change the requestmapping of getCities in @RequestMapping(value = "/{country:united-states|canada}-{state}") – Angelo Immediata Apr 20 '15 at 14:56
  • I can't change my URL pattern to be country-state. There are more complications in app if I do that. – minion Apr 20 '15 at 14:57
  • Your mappings are going to collide; you need to have some way to disambiguate these, e.g. `/cities/{country}/{state}` and `/details/{country}/{city}` – beerbajay Apr 20 '15 at 15:23
  • Why does Spring not honor the regex mapping i have provided? Any specific reason? – minion Apr 20 '15 at 15:26

1 Answers1

1

I'm not 100% sure @RequestMapping annotations support regex lookarounds but try excluding "united-states" and "canada" from the request mapping annotation for getDetailsInCity()

@RequestMapping(value = "/{country:^(?!.*canada)(?!.*united-states).*$}/{city}")
public String getDetailsInCity(@PathVariable String country,@PathVariable String city){
.....
}

I got the regex from here: How to negate specific word in regex?

Community
  • 1
  • 1
FriedSaucePots
  • 1,342
  • 10
  • 16
  • Although not a clean solution, this works. I will accept it as an answer if no other better answer pops up. Thanks. – minion Apr 20 '15 at 18:56