SpringBoot配置和使用矩阵变量

本文介绍了如何在Spring MVC中使用矩阵变量在session禁用时依然获取用户数据,通过配置`PathMatchConfigurer`并重写`configurePathMatch`方法,展示了如何解析`http://localhost:9999/userMsg/msg;name=java;age=22;pet=dog,cat,bird`这样的URL。
摘要由CSDN通过智能技术生成

首先配置

创建一个config类并且实现WebMvcConfigurer接口,然后重写configurePathMatch方法:

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }

接口

之后写接口:

@RequestMapping(value = "userMsg/{path}")
    public Map getUserMsg(@MatrixVariable("name") String userName,
                                         @MatrixVariable("age") int userAge,
                                         @MatrixVariable("pet") List<String> pet,
                                         @PathVariable("path") String path){
        Map<String,Object> map = new HashMap<>();
        map.put("name",userName);
        map.put("age",userAge);
        map.put("pet",pet);
        map.put("path",path);
        System.out.println(userName+" "+userAge+" "+pet);
        return map;
    }

写好后运行并且访问url:
http://localhost:9999/userMsg/msg;name=java;age=22;pet=dog,cat,bird
返回结果:
{“path”:“msg”,“name”:“java”,“age”:22,“pet”:“dog,cat,bird”}。

总结

矩阵变量可以用在session被禁用的时候,我们仍然能获取用户数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值