【问题标题】:Spring MVC Controller mapping regexSpring MVC 控制器映射正则表达式
【发布时间】:2016-09-16 16:32:02
【问题描述】:

我的控制器映射:

@RequestMapping(value = "{objectId}/{objectName: [a-zA-Z-]+}", method = GET)
public String getObjects(@PathVariable Integer objectId, Model model) { ... }

@RequestMapping(value = "{objectId}/{objectName: [a-zA-Z-]+}_{category}", method = GET)
public String getObjectsForCategories(@PathVariable Integer objectId, 
                                      @PathVariable String category, 
                                      Model model) { ... } 

还有我点击的网址:

http://localhost:8080/objects/2/xyz

http://localhost:8080/objects/2/xyz_mobile

Spring 无法找到处理程序并抱怨 No mapping found for HTTP 请求。

【问题讨论】:

  • 删除:regex之间的空格,然后重试。

标签: java spring spring-mvc


【解决方案1】:

根据Spring MVC documentation:

@RequestMapping注解支持使用正则表达式 在 URI 模板变量中。语法是{varName:regex},其中 第一部分定义变量名,第二部分 - 常规 表达。

因此,您应该删除:regex 之间的空格,如下所示:

@RequestMapping(value = "{objectId}/{objectName:[a-zA-Z-]+}", method = GET)
                                                ^ Space is removed

【讨论】:

    猜你喜欢
    • 2015-06-19
    • 1970-01-01
    • 2016-09-17
    • 2023-02-01
    • 1970-01-01
    • 2020-01-16
    • 2017-04-06
    • 2018-03-27
    • 2018-04-07
    相关资源
    最近更新 更多