[英]How to get a path variable that is the remainder of the path in Spring MVC?
我怎樣才能讓我的最后一個路徑變量成為剩余的路徑? 我有這樣的事情,但沒有受到打擊:
@RequestMapping(value = "{storageId}/{repositoryId}/{path}/**",
method = RequestMethod.PUT)
@RequestMapping(value = "{storageId}/{repositoryId}/{path}/**", method = RequestMethod.PUT)
public ResponseEntity upload(@PathVariable(name = "storageId") String storageId,
@PathVariable(name = "repositoryId") String repositoryId,
@PathVariable(name = "path") String path,
MultipartFile multipartFile)
throws ...
{
...
}
在澤西島,我可以像這樣輕松做到:
@Path("{storageId}/{repositoryId}/{path:.*}")
...但我必須將一些代碼遷移到 Spring MVC。
問題是,如果我的網址是:
http://localhost:48080/storages/storage0/snapshots/org/foo/bar/metadata/metadata-foo/3.1-SNAPSHOT/metadata-foo-3.1-20161017.182007-1.jar
我的path
被截斷為:
metadata/metadata-foo/3.1-SNAPSHOT/metadata-foo-3.1-20161017.182007-1.jar
這顯然是不正確的,因為它應該是:
org/foo/bar/metadata/metadata-foo/3.1-SNAPSHOT/metadata-foo-3.1-20161017.182007-1.jar
任何的建議都受歡迎!
@RequestMapping(value = "{storageId}/{repositoryId}/{path}/**",
method = RequestMethod.PUT)
public ResponseEntity upload(@PathVariable(name = "storageId") String storageId,
@PathVariable(name = "repositoryId") String repositoryId,MultipartFile multipartFile,HttpServletRequest request)
throws ...
{
String restOfTheUrl = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
}
使用 Spring 5 中開始出現的新(ish)匹配算法很容易。您只需使用{*foo}
作為路徑模式。
@RequestMapping(value = "{storageId}/{repositoryId}/{path}/{*rest}",
method = RequestMethod.PUT)
public ResponseEntity upload(@PathVariable(name = "storageId") String storageId,
@PathVariable(name = "repositoryId") String repositoryId,
@PathVariable(name = "path") String path,
@PathVariable(name = "rest") String rest,
MultipartFile multipartFile)
throws ...
{
...
}
這在 Webflux 中默認有效。 對於 MVC,您必須使用spring.mvc.pathmatch.matching-strategy=path_pattern_parser
打開PathPattern
匹配器。 我知道這將是 Spring Boot 2.6 中的默認設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.