框架:springboot
背景:
删除操作用的是mvc的rest操作。
@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET, produces = {MediaType.xxx})
public Map delete(@PathVariable("id") String id) {
但是id字符串中含有.,就造成了参数id信息获得错误。
原因:
spring会默认自动截取掉最后一个点后面的字符串
解决方案:
1.做适配,只针对于当前url
@RequestMapping(value = "/delete/{id:.*}", method = RequestMethod.GET, produces = {MediaType.xxx})
public Map delete(@PathVariable("id") String id) {
2.修改默认配置,针对整个工程,boot中默认注册的.json和.xml还是会通过
@Configuration
public class DemoWebMvcConfigurer extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer matcher) {
matcher.setUseRegisteredSuffixPatternMatch(true);
}
}
3.修改默认配置,针对整个工程
@Configuration
public class DemoWebMvcConfigurer extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer matcher) {
matcher.setUseSuffixPatternMatch(false);
}
}
4.既然只截取最后一个,在url后面一个不带点的字符串。
@RequestMapping(value = "/delete/{id}/demo", method = RequestMethod.GET, produces = MediaType.xxx})
public Map delete(@PathVariable("id") String id) {
注意:如果方案3中设置false了,方案2中设置true。方案3设置失效。
sprigboot最新版本的不存在此问题,请问上面2,3的配置,最最新版本全都是false。
自己想知道原理的,请跟下源码。这里就不再介绍。如果有什么不明白的@我。