springboot项目中url中出在点的情况

框架: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。

自己想知道原理的,请跟下源码。这里就不再介绍。如果有什么不明白的@我。


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值