我正在尝试使用带有Spring-Boot应用程序的WebJars-Locator来映射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
但是这个根本没有映射到处理程序,只是返回404:
http://localhost/webjars-locator/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css
根本区别在于路径中的组件数量应该由正则表达式(“。+”)处理,但当该部分具有斜杠时似乎不起作用。< / p>
如果有帮助,则会在日志中提供: 2015-03-03 23:03:53.588 INFO 15324 --- [main] swsmmaRequestMappingHandlerMapping:Mapped“{[/webjars-locator/ {webjar} / {partialPath:。+}],method#= Get] ,params = [],headers = [],consume = [],produce = [],custom = []}“on public org.springframework.http.ResponseEntity app.controllers.WebJarsLocatorController.locateWebjarAsset(java.lang.String, java.lang.String中)
2 Spring-Boot中是否存在某种类型的隐藏设置以在RequestMappings上启用正则表达式模式匹配?
答案 0 :(得分:8)
文档中的原始代码并没有为额外的斜杠做好准备,对不起!
请尝试使用此代码:
@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处理
答案 1 :(得分:1)
您似乎无法使用PathVariable
来匹配“网址的剩余部分”。你必须使用蚂蚁风格的路径模式,即“**”,如下所述:
Spring 3 RequestMapping: Get path value
然后,您可以获取请求对象的完整URL并提取“剩余部分”。