Spring MVC Controller映射正则表达式

时间:2016-05-19 16:35:57

标签: java spring spring-mvc

我的控制器映射:

@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无法找到处理程序并抱怨找不到HTTP请求的映射。

1 个答案:

答案 0 :(得分:3)

根据http://neo4j.com/docs/stable/ha-configuration.html#config_dbms.security.ha_status_auth_enabled

  

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

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

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