0

I am using Spring MVC to build the RESTful APIs like:

@RequestMapping(value ="/session={sessionID}&p1={p1}&p2={p2}")
public @ResponseBody
Object getData1(@PathVariable String sessionID,
        @PathVariable String p1, @PathVariable String p2) {

    return "Get Data";
}

@RequestMapping(value ="/session={sessionID}&p1={p1}&p2={p2}&p3={p3}")
public @ResponseBody
Object getData2(@PathVariable String sessionID,
        @PathVariable String p1, @PathVariable String p2, @PathVariable String p3) {

    return "Get next Data";
}

But, when I type the url as /session=1&p1=a&p2=b and /session=1/&p1=a&p2=b&p3=c, they always return the same string "Get Data". While debugging, I found that both requests went to the same first method and p2="b&p3=c". It really confuses me :(

Need your help. Thanks. -C

Kyleinincubator
  • 313
  • 1
  • 5
  • 12
  • Your URLs look like parameter lists but lack the "?" separator... seems like an odd thing to do. Why not use regular REST URLs like `/session/{sessionId}` and then map the parameters via `@RequestParam()`? E.g. URL would be `/session/1?p1=a&p2=b`. – nickdos Mar 21 '12 at 08:58
  • See http://stackoverflow.com/q/5000876/249327 for example of grabbing params via @PathVariable – nickdos Mar 21 '12 at 09:08
  • you are quite right. I am using the @RequestParam and it is working well for me now. Thanks. – Kyleinincubator Mar 21 '12 at 19:52

1 Answers1

0

You could work around unsupported 'overloading' by using different patterns like

"/session={sessionID}/data1/&p1={p1}&p2={p2}"

and

"/session={sessionID}/data2&p1={p1}&p2={p2}}&p3={p3}"
stacker
  • 68,052
  • 28
  • 140
  • 210
  • Could you give me more details why I can not do that? – Kyleinincubator Mar 20 '12 at 23:00
  • It takes the first one that is a match. Nothing says 'b&p3=c' isn't a perfectly legal value for p2, so it doesn't keep trying more to see if there's a 'better' match. – Affe Mar 20 '12 at 23:10
  • It is configurable btw if you provide your own implementation of org.springframework.util.PathMatcher – Affe Mar 20 '12 at 23:37