我想创建一个GET将文件路径作为路径变量的请求。
正如 Spring 文档中所述,发现here https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/util/pattern/PathPattern.html,这应该可以通过使用以下内容实现:/resources/{*path}。
我正在使用 Spring Boot 2.1.2,它使用 Spring 5。
但是,当我像这样设置控制器方法时,请求与路由不匹配。例如,预期的匹配路径是 /resources/some/filepath ,这应该导致 PathVariable“路径”为 /some/filepath
@GetMapping("/resources/{*path}")
public String content(@PathVariable String path) {
return null;
}
我没有找到有关使用新 PathPattern 所需的任何配置的任何信息。我发现的有关此新功能的唯一其他信息是 Baeldung 上的一篇帖子(https://www.baeldung.com/spring-5-mvc-url-matching https://www.baeldung.com/spring-5-mvc-url-matching)其中没有提及任何有关配置的内容。所以我希望它可以开箱即用,但事实并非如此。
我克隆了 Baeldung 帖子中提到的项目。相应的单元测试运行。当我将控制器方法和单元测试复制到我的项目时,它失败了。所以我认为这与配置有关。
感谢您的任何帮助。
In the 常见应用程序属性 https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#common-application-properties-web在Spring文档中有一个属性叫做spring.mvc.pathmatch.匹配策略,用作“根据注册映射匹配请求路径的策略选择”。
默认值(截至目前)是蚂蚁路径匹配器,并且由于您想使用 PathPattern,因此您需要将其写入 application.properties 文件中:
spring.mvc.pathmatch.matching-strategy=路径模式解析器
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)