Spring 3.0 多个@PathVariable的问题

发布于 2024-11-29 05:47:31 字数 729 浏览 0 评论 0原文

在我的应用程序中,我必须比较控制器中的 3 个产品,

@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

当点击我的 url(http://something/products/12Vs13Vs14) 时,

我映射了请求,我收到 http 400 错误,

我也尝试了 2 个 @pathVariable,如下

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

所示工作正常,但为什么我面临 3 个变量的问题,而且服务器日志中也没有错误,那么如何找到错误所在。

有什么解决办法吗??

In my application I have to compare 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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟─花易冷 2024-12-06 05:47:31

如何显式指定您希望每个路径变量匹配的正则表达式,如所述 这里

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

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)

來不及說愛妳 2024-12-06 05:47:31

您可以尝试将所有内容集中到一个路径变量中,然后手动解析它:

@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

不过,这更多的是一种解决方法,而不是解决方案。如果您想尝试找出到底出了什么问题,您可能需要按照 Bozho 的建议在 Spring 代码中进行调试。

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.

人海汹涌 2024-12-06 05:47:31
  • 确保问题不在响应中 - 在方法中放置断点并查看是否调用它
  • 检查日志文件中是否有任何指示
  • 尝试使用斜杠作为分隔符 /products/{p1}/{p2}/{p3}/products/{p1}/vs/{p2}/vs/{p3}
  • 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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文