springboot-1.3.5 --> spring-core-4.2.6.release
springboot-1.5.7 --> spring-core-4.3.11.release
问题描述
项目中有一个请求路径的映射末尾有一个空格,导致从springboot-1.3.5升级到1.5.7后,报404错误
示例代码
@RequestMapping(value="/query ",method=RequestMethod.GET)
public void query(){
return null;
}
在query后多了个一个空格符
在原版本中,项目运行无误,但是升级后该路径就报404了
问题分析
经过一番debug,终于发现,是spring-core包中的一个类有了变化
AntPathMatcher
在该类的方法tokenizePath
中发现了问题所在
/**
* Tokenize the given path pattern into parts, based on this matcher's settings.
* <p>Performs caching based on {@link #setCachePatterns}, delegating to
* {@link #tokenizePath(String)} for the actual tokenization algorithm.
* @param pattern the pattern to tokenize
* @return the tokenized pattern parts
*/
protected String[] tokenizePattern(String pattern) {
String[] tokenized = null;
Boolean cachePatterns = this.cachePatterns;
if (cachePatterns == null || cachePatterns.booleanValue()) {
tokenized = this.tokenizedPatternCache.get(pattern);
}
if (tokenized == null) {
tokenized = tokenizePath(pattern);
if (cachePatterns == null && this.tokenizedPatternCache.size() >=
CACHE_TURNOFF_THRESHOLD) {
// Try to adapt to the runtime situation that we're encountering:
// There are obviously too many different patterns coming in here...
// So let's turn off the cache since the patterns are unlikely to be reoccurring.
deactivatePatternCache();
return tokenized;
}
if (cachePatterns == null || cachePatterns.booleanValue()) {
this.tokenizedPatternCache.put(pattern, tokenized);
}
}
return tokenized;
}
/**
*Tokenize the given path String into parts, based on this matcher's
settings.
* @param path the path to tokenize
* @return the tokenized path parts
*/
protected String[] tokenizePath(String path) {
return StringUtils.tokenizeToStringArray(path, this.pathSeparator, this.trimTokens, true);
}
在方法tokenizePath
中传递了一个属性trimTokens
,这个属性在spring-core-4.2.6.release中是默认的true
而该属性在spring-core-4.3.11.release中是默认的false
这就导致在执行org.springframework.util.StringUtils的tokenizeToStringArray
方法时做了去空格的不同判断
具体这个属性的变化历史,我这里就没有去追究了,已经解决了我的疑惑咯