2013-07-26 22 views
1

我試圖使用請求映射到匹配的網址是這樣的:如何在RequestMapping中使用正則表達式路徑映射變量?

http://localhost:9999/si/configs/some/path/on/the/server/file.xml 

我已經建立了這樣的控制器,但findConfig方法RequestMapping的值不正確。這裏是我的代碼(剝離乾淨一切外來的):

@Controller 
@RequestMapping("/si/configs") 
public class SIController { 

    @RequestMapping(value = "{path:/**/*.xml}", method = RequestMethod.GET) 
    public ResponseEntity<String> findConfig(@PathVariable("path") String path) { 
     // value of path arg should be: "/some/path/on/the/server/file.xml" 
    } 
} 

我知道,Spring允許你正則表達式的路徑字符串匹配,我試圖按照這一問題的建議:Spring URI Template Patterns with Regular Expressions,但我似乎無法得到這個正確的。

請求映射中應該怎麼做?


編輯

當我刪除路徑變量和去與這樣的:預期

@RequestMapping(value = "/**/*.xml", method = RequestMethod.GET) 
    public ResponseEntity<String> findConfig() { 
    } 

我的控制器方法被調用。所以,有一些關於嘗試匹配一個路徑變量與春天不喜歡的野生crads。

+0

如果你確信你的路徑映射是正確的,要確保將.xml後綴不是由Spring內容協商剝離。 – Jukka

+0

當我調試時,xml後綴仍然存在。我只是不確定路徑變量中的正確語法。 –

+0

編輯該問題以添加更多信息。 –

回答

1

如何使用HttpServletRequest direclty?

@RequestMapping(value = "/**/*.xml", method = RequestMethod.GET) 
public ResponseEntity<String> findConfig(HttpServletRequest request) { 
    String path = request.getServletPath()/getRequestURL()/getPathInfo() 
    //I forget which one could give the correct part, maybe some parse operation needed 

}

+0

這可能會起作用。我必須嘗試一下。 –

相關問題