Spring-使用正则表达式匹配启动@RequestMapping 和@PathVariable
Spring-Boot @RequestMapping and @PathVariable with regular expression matching
我正在尝试将 WebJars-Locator 与 Spring-Boot 应用程序一起使用以映射 JAR 资源。根据他们的 website,我创建了一个这样的 RequestMapping:
@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/webjars-locator/{webjar}/{partialPath:.+}")
public ResponseEntity<ClassPathResource> locateWebjarAsset(@PathVariable String webjar, @PathVariable String partialPath)
{
问题是 partialPath 变量应该包含第三个斜杠后的任何内容。然而,它最终做的是限制映射本身。此 URI 已正确映射:
http://localhost/webjars-locator/angular-bootstrap-datetimepicker/datetimepicker.js
但是这个根本没有映射到处理程序,只是 returns 一个 404:
http://localhost/webjars-locator/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css
根本区别只是路径中的组件数量,应该 由正则表达式 (".+") 处理,但在 时似乎不起作用部分有斜线。
如果有帮助,请在日志中提供:
2015-03-03 23:03:53.588 INFO 15324 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/webjars-locator/{webjar}/{partialPath:.+}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity app.controllers.WebJarsLocatorController.locateWebjarAsset(java.lang.String,java.lang.String)
2
Spring-Boot 中是否有某种类型的隐藏设置以在 RequestMappings 上启用正则表达式模式匹配?
您似乎无法使用 PathVariable
来匹配 "the remaining part of the url"。你必须使用蚂蚁风格的路径模式,即“**”,如下所述:
Spring 3 RequestMapping: Get path value
然后您可以获得请求对象的整个 URL 并提取 "remaining part"。
文档中的原始代码没有为额外的斜线做好准备,对此深表歉意!
请尝试使用此代码:
@ResponseBody
@RequestMapping(value="/webjarslocator/{webjar}/**", method=RequestMethod.GET)
public ResponseEntity<Resource> locateWebjarAsset(@PathVariable String webjar,
WebRequest request) {
try {
String mvcPrefix = "/webjarslocator/" + webjar + "/";
String mvcPath = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
String fullPath = assetLocator.getFullPath(webjar,
mvcPath.substring(mvcPrefix.length()));
ClassPathResource res = new ClassPathResource(fullPath);
long lastModified = res.lastModified();
if ((lastModified > 0) && request.checkNotModified(lastModified)) {
return null;
}
return new ResponseEntity<Resource>(res, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
我还将很快提供 webjar 文档的更新。
2015/08/05 更新:添加了 If-Modified-Since 处理
我正在尝试将 WebJars-Locator 与 Spring-Boot 应用程序一起使用以映射 JAR 资源。根据他们的 website,我创建了一个这样的 RequestMapping:
@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/webjars-locator/{webjar}/{partialPath:.+}")
public ResponseEntity<ClassPathResource> locateWebjarAsset(@PathVariable String webjar, @PathVariable String partialPath)
{
问题是 partialPath 变量应该包含第三个斜杠后的任何内容。然而,它最终做的是限制映射本身。此 URI 已正确映射:
http://localhost/webjars-locator/angular-bootstrap-datetimepicker/datetimepicker.js
但是这个根本没有映射到处理程序,只是 returns 一个 404:
http://localhost/webjars-locator/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css
根本区别只是路径中的组件数量,应该 由正则表达式 (".+") 处理,但在 时似乎不起作用部分有斜线。
如果有帮助,请在日志中提供:
2015-03-03 23:03:53.588 INFO 15324 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/webjars-locator/{webjar}/{partialPath:.+}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity app.controllers.WebJarsLocatorController.locateWebjarAsset(java.lang.String,java.lang.String) 2
Spring-Boot 中是否有某种类型的隐藏设置以在 RequestMappings 上启用正则表达式模式匹配?
您似乎无法使用 PathVariable
来匹配 "the remaining part of the url"。你必须使用蚂蚁风格的路径模式,即“**”,如下所述:
Spring 3 RequestMapping: Get path value
然后您可以获得请求对象的整个 URL 并提取 "remaining part"。
文档中的原始代码没有为额外的斜线做好准备,对此深表歉意!
请尝试使用此代码:
@ResponseBody
@RequestMapping(value="/webjarslocator/{webjar}/**", method=RequestMethod.GET)
public ResponseEntity<Resource> locateWebjarAsset(@PathVariable String webjar,
WebRequest request) {
try {
String mvcPrefix = "/webjarslocator/" + webjar + "/";
String mvcPath = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
String fullPath = assetLocator.getFullPath(webjar,
mvcPath.substring(mvcPrefix.length()));
ClassPathResource res = new ClassPathResource(fullPath);
long lastModified = res.lastModified();
if ((lastModified > 0) && request.checkNotModified(lastModified)) {
return null;
}
return new ResponseEntity<Resource>(res, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
我还将很快提供 webjar 文档的更新。
2015/08/05 更新:添加了 If-Modified-Since 处理