2016-05-19 123 views
2

我的控制器的映射: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) { ... } 

,並且URL我打:

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

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

春找不到處理程序並抱怨H沒有找到映射TTP請求。

+0

刪除'之間的空間:和''regex'並再試一次。 –

回答

3

按照Spring MVC documentation:

@RequestMapping註解支持使用正則表達式 在URI模板變量。語法是{varName:regex},其中 第一部分定義變量名稱,第二部分定義 表達式。

所以,你應該刪除空格:regex之間,如下所示:

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