Spring MVC 控制器映射正则表达式

4

我的控制器映射:

@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) { ... } 

我要翻译的内容是关于IT技术的,并涉及以下url:

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

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

在Spring框架中,出现了无法找到处理程序的问题,并提示“找不到HTTP请求的映射”。


移除 :regex 之间的空格,然后重试。 - Ali Dehghani
1个回答

4
根据Spring MVC文档@RequestMapping注解支持在URI模板变量中使用正则表达式。语法为{varName:regex},其中第一部分定义变量名称,第二部分定义正则表达式。因此,您应该删除:regex之间的空格,如下所示:
@RequestMapping(value = "{objectId}/{objectName:[a-zA-Z-]+}", method = GET)
                                                ^ Space is removed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接