rob*_*606 2 regex spring spring-mvc webjars spring-boot
我正在尝试使用带有Spring-Boot应用程序的WebJars-Locator来映射JAR资源.根据他们的网站,我创建了一个这样的RequestMapping:
@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/webjars-locator/{webjar}/{partialPath:.+}")
public ResponseEntity<ClassPathResource> locateWebjarAsset(@PathVariable String webjar, @PathVariable String partialPath)
{
Run Code Online (Sandbox Code Playgroud)
这个问题是partialPath变量应该包含第三个斜杠之后的任何内容.然而,它最终做的是限制映射本身.此URI已正确映射:
http://localhost/webjars-locator/angular-bootstrap-datetimepicker/datetimepicker.js
Run Code Online (Sandbox Code Playgroud)
但是这个根本没有映射到处理程序,只返回404:
http://localhost/webjars-locator/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css
Run Code Online (Sandbox Code Playgroud)
根本区别仅在于路径中应由正则表达式(".+")处理的组件数,但在该部分具有斜杠时似乎不起作用.
如果有帮助,则会在日志中提供:
2015-03-03 23:03:53.588 INFO 15324 --- [main] swsmmaRequestMappingHandlerMapping:映射"{[/ webjars-locator/{webjar}/{partialPath:.+}],methods = [GET],params = [ ],headers = [],consume = [],produce = [],custom = []}"on public org.springframework.http.ResponseEntity app.controllers.WebJarsLocatorController.locateWebjarAsset(java.lang.String,java.lang.字符串)2
Spring-Boot中是否存在某种类型的隐藏设置以在RequestMappings上启用正则表达式模式匹配?
文档中的原始代码没有为额外的斜杠准备,对不起!
请尝试使用此代码:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
我还将简要介绍webjar文档的更新.
更新2015/08/05:添加了If-Modified-Since处理
| 归档时间: |
|
| 查看次数: |
10209 次 |
| 最近记录: |