开发者

Spring 3.0 multiple @PathVariable's problem

开发者 https://www.devze.com 2023-03-27 03:32 出处:网络
In my application I have to compar开发者_开发技巧e 3 products for that in my controller I mapped request as

In my application I have to compar开发者_开发技巧e 3 products for that in my controller I mapped request as

@RequestMapping(value = "/products/{proId1}Vs{proId2}Vs{proId3}", method = RequestMethod.GET)
public ModelAndView compareThreeProducts(@PathVariable("proId1") int id1, @PathVariable("proId2") int id2, @PathVariable("proId3") int id3)
{
   //someLogic

when hit my url(http://something/products/12Vs13Vs14)

I'm getting http 400 error

I also tried for 2 @pathVariable like

@RequestMapping(value = "/products/{proId1}Vs{proId2}", method = RequestMethod.GET)
public ModelAndView compareTwoProducts(@PathVariable("proId1") int id1, @PathVariable("proId2") int id2) 

this is working fine but why i'm facing problem with 3 variables and also there are no errors in server log then how to find what's the bug.

any solution??


How about explicitly specifying the regex you want each path variable to match, as described here?

@RequestMapping(value = "/products/{proId1:\d+}Vs{proId2:\d+}Vs{proId3:\d+}", method = RequestMethod.GET)


You could try lumping everything into one path variable then parsing it manually:

@RequestMapping(value = "/products/{compareIdString}", method = RequestMethod.GET)
public ModelAndView compareThreeProducts(@PathVariable("compareIdString") String compareIdString)
{
    // split compareIdString on "Vs"
    // parse each resulting value to an int

This is more of a workaround than a solution, though. You might want to debug in the Spring code as Bozho suggested if you want to try to figure out exactly what's going wrong.


  • make sure the problem is not in the response - put a breakpoint in the method and see if it is invoked
  • check log files for any indications
  • try using slashes as separator /products/{p1}/{p2}/{p3} or /products/{p1}/vs/{p2}/vs/{p3}
0

精彩评论

暂无评论...
验证码 换一张
取 消