- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
通过URL的后缀发现了Spring中的useSuffixPatternMatch这个参数
spring-boot的版本是2.1.4.RELEASE,spring的版本是5.1.6.RELEASE
一个例子如下:
@Configuration
@Import(WebMvcAutoConfiguration.EnableWebMvcConfiguration.class)
@SuppressWarnings("unchecked")
public class WebConfig implements WebMvcConfigurer, WebMvcRegistrations {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new RequestMappingHandlerMapping();
}
}
@RestController
public class ParamController {
@GetMapping(value = "/param/{param1}")
public String param(@PathVariable("param1") String param1) {
return param1;
}
}
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
启动一下,访问http://127.0.0.1:8080/param/hehe
和http://127.0.0.1:8080/param/hehe.hehe
都返回hehe
如果访问http://127.0.0.1:8080/param/hehe.hehe.hehe
,它会返回hehe.hehe
所以会发现它把最后一个小数点后面的字符给截掉了,那如果我们想要获取完整的字符串,该怎么办呢?
下面的例子都是根据http://127.0.0.1:8080/param/hehe.hehe这个url来调试的
入口在InvocableHandlerMethod.invokeForRequest
,如下:
是根据PathVariableMethodArgumentResolver.resolveName
得来的,如下:
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".uriTemplateVariables";
什么时候放进attributes
里的?,在RequestMappingInfoHandlerMapping.handleMatch
,如下:
程序定义的:/param/{param1} -> /param/{param1}.*
接口传过来的:/param/hehe.hehe
以/分割,第一个字符串param里没有参数,所以会跳过,直接看第二个字符串:
{param1}.* -> pattern=(.*)\Q.\E.*
hehe.hehe
param1=hehe
我们定义的是/param/{param1}
,怎么就变成了/param/{param1}.*
?在PatternsRequestCondition.getMatchingPattern
可以看到如果useSuffixPatternMatch为true,并且指定的url里没有.,会在后缀自动增加.*
public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping
implements MatchableHandlerMapping, EmbeddedValueResolverAware {
private boolean useSuffixPatternMatch = true;
@Override
public void afterPropertiesSet() {
this.config = new RequestMappingInfo.BuilderConfiguration();
this.config.setUrlPathHelper(getUrlPathHelper());
this.config.setPathMatcher(getPathMatcher());
this.config.setSuffixPatternMatch(this.useSuffixPatternMatch); // 这里设置了
this.config.setTrailingSlashMatch(this.useTrailingSlashMatch);
this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch);
this.config.setContentNegotiationManager(getContentNegotiationManager());
super.afterPropertiesSet();
}
}
WebConfig
的getRequestMappingHandlerMapping
@Configuration
@Import(WebMvcAutoConfiguration.EnableWebMvcConfiguration.class)
@SuppressWarnings("unchecked")
public class WebConfig implements WebMvcConfigurer, WebMvcRegistrations {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
RequestMappingHandlerMapping requestMappingHandlerMapping = new RequestMappingHandlerMapping();
requestMappingHandlerMapping.setUseSuffixPatternMatch(false);
return requestMappingHandlerMapping;
}
}
configurePathMatch
方法@Configuration
@Import(WebMvcAutoConfiguration.EnableWebMvcConfiguration.class)
@SuppressWarnings("unchecked")
public class WebConfig implements WebMvcConfigurer, WebMvcRegistrations {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new RequestMappingHandlerMapping();
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
}
}
a. 中间的参数是不受影响的
@RestController
public class ParamController {
@GetMapping(value = "/param/{param1}/{param2}")
public String param(@PathVariable("param1") String param1, @PathVariable("param2") String param2) {
return param1 + " " + param2;
}
}
访问http://127.0.0.1:8080/param/hehe.hehe/hehe.hee
返回hehe.hehe hehe
b. 增加点
@RestController
public class ParamController {
//@GetMapping(value = "/param/{param1}")
//public String param(@PathVariable("param1") String param1) {
// return param1;
//}
@GetMapping(value = "/param/{param1}.{param2}")
public String param(@PathVariable("param1") String param1, @PathVariable("param2") String param2) {
return param1 + " " + param2;
}
}
访问http://127.0.0.1:8080/param/hehe.hehe
返回hehe hehe
注意第一个方法和第二个方法不要同时出现,如果同时出现的话,则会访问第一个方法
@RestController
public class ParamController {
@GetMapping(value = "/param/{param1}")
public String param(@PathVariable("param1") String param1) {
return param1;
}
@GetMapping(value = "/param/{param1}.{param2}")
public String param(@PathVariable("param1") String param1, @PathVariable("param2") String param2) {
return param1 + " " + param2;
}
}
访问http://127.0.0.1:8080/param/hehe.heh
返回hehe
@RestController
public class ParamController {
@GetMapping(value = "/param/{param1:.+}")
public String param(@PathVariable("param1") String param1) {
return param1;
}
}
访问http://127.0.0.1:8080/param/hehe.hehe
,返回hehe.hehe
原因如下:
/param/{param1:.+} -> /param/{param1:.+}
/param/hehe.hehe
{param1:.+} -> pattern=(.+)
hehe.hehe
param1=hehe.hehe
得到pattern以及提取参数的类 AntPathStringMatcher
protected static class AntPathStringMatcher {
private static final Pattern GLOB_PATTERN = Pattern.compile("\\?|\\*|\\{((?:\\{[^/]+?\\}|[^/{}]|\\\\[{}])+?)\\}");
private static final String DEFAULT_VARIABLE_PATTERN = "(.*)";
private final Pattern pattern;
private final List<String> variableNames = new LinkedList<>();
public AntPathStringMatcher(String pattern) {
this(pattern, true);
}
public AntPathStringMatcher(String pattern, boolean caseSensitive) {
StringBuilder patternBuilder = new StringBuilder();
Matcher matcher = GLOB_PATTERN.matcher(pattern);
int end = 0;
while (matcher.find()) {
patternBuilder.append(quote(pattern, end, matcher.start()));
String match = matcher.group();
if ("?".equals(match)) {
patternBuilder.append('.');
}
else if ("*".equals(match)) {
patternBuilder.append(".*");
}
else if (match.startsWith("{") && match.endsWith("}")) {
int colonIdx = match.indexOf(':');
if (colonIdx == -1) {
patternBuilder.append(DEFAULT_VARIABLE_PATTERN);
this.variableNames.add(matcher.group(1));
}
else {
String variablePattern = match.substring(colonIdx + 1, match.length() - 1);
patternBuilder.append('(');
patternBuilder.append(variablePattern);
patternBuilder.append(')');
String variableName = match.substring(1, colonIdx);
this.variableNames.add(variableName);
}
}
end = matcher.end();
}
patternBuilder.append(quote(pattern, end, pattern.length()));
this.pattern = (caseSensitive ? Pattern.compile(patternBuilder.toString()) :
Pattern.compile(patternBuilder.toString(), Pattern.CASE_INSENSITIVE));
}
private String quote(String s, int start, int end) {
if (start == end) {
return "";
}
return Pattern.quote(s.substring(start, end));
}
/**
* Main entry point.
* @return {@code true} if the string matches against the pattern, or {@code false} otherwise.
*/
public boolean matchStrings(String str, @Nullable Map<String, String> uriTemplateVariables) {
Matcher matcher = this.pattern.matcher(str);
if (matcher.matches()) {
if (uriTemplateVariables != null) {
// SPR-8455
if (this.variableNames.size() != matcher.groupCount()) {
throw new IllegalArgumentException("The number of capturing groups in the pattern segment " +
this.pattern + " does not match the number of URI template variables it defines, " +
"which can occur if capturing groups are used in a URI template regex. " +
"Use non-capturing groups instead.");
}
for (int i = 1; i <= matcher.groupCount(); i++) {
String name = this.variableNames.get(i - 1);
String value = matcher.group(i);
uriTemplateVariables.put(name, value);
}
}
return true;
}
else {
return false;
}
}
}
https://www.cnblogs.com/yougatei/p/12812906.html
具体详细介绍请看下文: 在使用文件进行交互数据的应用来说,使用FTP服务器是一个很好的选择。本文使用Apache Jakarta Commons Net(commons-net-3.3.jar)
我在日志文件中收到这些警告: WARN 2013-01-15 00:08:15,550 org.eclipse.jetty.http.HttpParser- HttpParser Full for
我在使用特定网页时遇到问题。当我按下链接时,我收到应用程序错误(不是 http 错误等,而是应用程序级别错误)。 但是我打开了开发人员工具和网络控制台,我看到没有请求发送到服务器。 所以我双击并选择查
我没有组装经验,但这是我一直在做的。如果在通过程序集中的指针传递参数和调用函数时缺少任何基本方面,我希望输入。 例如,我想知道是否应该还原ecx,edx,esi,edi,。我读到它们是通用寄存器,但我
我没有组装经验,但这是我一直在做的。如果在通过程序集中的指针传递参数和调用函数时缺少任何基本方面,我希望输入。 例如,我想知道是否应该还原ecx,edx,esi,edi,。我读到它们是通用寄存器,但我
我正在尝试创建完整 uiscrollview 的快照,所有内容大小,我已经搜索了很多,并且我在 SO 上找到了一些东西,如下所示: Getting a screenshot of a UIScroll
我想复制一个包含以下结构的Vector,对我来说重要的是在修改复制的 vector 时保持原始Vector完整: public class objet_poid_n { public int
给定一个示例字符串 s = '嗨,我的名字是 Humpty-Dumpty,来自“爱丽丝,爱丽丝镜中奇遇记”',我想将其分成以下 block : # To Do: something like {l =
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我正在尝试创建一个正则表达式来查找文本中的 Linux 文件路径,但是正则表达式对我来说非常陌生。我有下面的代码片段,它将识别下面文件结构的开头。 .*(/bin/|/home/).* 完成正则表达式
我正在寻找远程托管的 JPG 的尺寸、宽度和高度。我已经了解了如何通过下载完整图像来执行此操作。 但是,如果我可以通过仅下载足以获取此信息的方式来做到这一点,那将是理想的。 典型的图像大小为 200K
有没有办法让下面的代码: import traceback def log(message): print "%s: %s" %(traceback.extract_stack()[0:-1]
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
git show 显示了修订版中所做的所有更改。但是,它会打印出所有更改——而不仅仅是文件名。 git show --stat 只显示文件名,但它把它们截断了!有没有办法获得已更改文件名的完整列表?
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想要改善这个问题吗?更新问题,以便将其作为on-topi
当我在模板中调用我的模型 get_absolute_url 方法时,我想要一个绝对/完整的 url。在我的入门模型中,我有以下内容: def get_absolute_url(self): r
我正在使用 jQuery 1.5.1 这是我的代码: $('.cellcontent').animate({ left: '-=190'}, { easing: alert('start
我正在使用下面的方法删除条形图并使用新数据更新条形图,但这样做时出现了一个小故障/完整的图表消失 1 秒,直到加载新数据。但是是否可以通过仅增加/减少柱形而不实际消失图表来实现相同的目的。 d3.se
基于 this question 中的讨论,任何人都可以提供代码或代码链接,显示 NumericLiteralX 模块的完整实现(例如 this one )?我对 NumericLiteralX 模块
我的目标是检索网站的 html,并将其转换为可读的String。我下面的代码可以工作,但我遇到了一个技术问题:当我尝试检索 http://time.gov/HTML5 的 html 时,我在 andr
我是一名优秀的程序员,十分优秀!