如何应用 Spring 5 中引入的 PathPatternParser? [英] How to apply the PathPatternParser introduced in Spring 5?

查看:131
本文介绍了如何应用 Spring 5 中引入的 PathPatternParser?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个将文件路径作为路径变量的 GET 请求.

如 Spring 文档中所述,发现 此处,这应该可以通过使用以下内容:/resources/{*path}.

我使用的是使用 Spring 5 的 Spring Boot 2.1.2.

但是,当我像这样设置控制器方法时,请求与路由不匹配.预期的匹配路径将是例如/resources/some/filepath 这应该导致 PathVariable路径"为/some/filepath

 @GetMapping("/resources/{*path}")公共字符串内容(@PathVariable 字符串路径){返回空;}

我没有找到有关使用新 PathPattern 所需的任何配置的任何信息.我发现的有关此新功能的唯一其他信息是 Baeldung (https://www.baeldung.com/spring-5-mvc-url-matching) 中没有提到任何关于配置的内容.所以我希望它可以开箱即用,但事实并非如此.

我克隆了 Baeldung 帖子中提到的项目.相应的单元测试运行.当我将 Controller 方法和单元测试复制到我的项目时,它失败了.所以我希望它与配置有关.

感谢您的帮助.

解决方案

Common Application properties 在 Spring 文档中有一个名为 spring.mvc.pathmatch.matching-strategy 的属性,用作根据注册映射匹配请求路径的策略选择".

默认值(截至目前)是 ant-path-matcher,由于您想使用 PathPattern,您需要在 application.properties 文件中写入:>

spring.mvc.pathmatch.matching-strategy=path-pattern-parser

I want to create a GET request that takes a filepath as a path variable.

As described in the Spring documentation found here, this should be possible by using the following: /resources/{*path}.

I am using Spring Boot 2.1.2 which uses Spring 5.

However when I set up my controller method like this, the request doesn't match the route. An expected matching path would be for example /resources/some/filepath which should lead the PathVariable "path" to be /some/filepath

  @GetMapping("/resources/{*path}")
  public String content(@PathVariable String path) {
    return null;
  }

I didn't find any information about any configuration that is needed in order to make use of the new PathPattern. The only other piece of information I found about this new feature is a post at Baeldung (https://www.baeldung.com/spring-5-mvc-url-matching) which doesn't mention anything about configuration. So I expect that it should work out of the box, but it doesn't.

I cloned the project mentioned in the Baeldung post. The corresponding unit tests run. When I copy the Controller method and the unit test to my project, it fails. So I expect that it has to do with configuration.

Thank you for any help.

解决方案

In the Common Application properties in the Spring documentation there's a property called spring.mvc.pathmatch.matching-strategy, that's used as "Choice of strategy for matching request paths against registered mappings".

The default value (up to this date) is ant-path-matcher, and since you want to use PathPattern, you'll need to write this in the application.properties file:

spring.mvc.pathmatch.matching-strategy=path-pattern-parser

这篇关于如何应用 Spring 5 中引入的 PathPatternParser?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆