SpringBoot学习笔记(三) 矩阵变量 @MatrixVariable 注解使用和理解

SpringBoot学习笔记(三) 矩阵变量 @MatrixVariable 注解使用和理解

SpringBoot @MatrixVariable 注解的用法

示例一

​ 假设前端的请求路径为

/cars/sell;low=34;brand=byd,aodi,yd

​ 那么我们Controller应该这样接收参数

 @GetMapping("/cars/{path}")
    public Map carsSell(@MatrixVariable("low") Integer low,
                        @MatrixVariable("brand") List<String> brand,
                        @PathVariable("path") String path){
        ......
    }
示例二

​ 前端请求路径为

/boss/1;age=20/2;age=10

​ 那么我们Controller应该这样接收参数

@GetMapping("/boss/{bossId}/{empId}")
    public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,
                    @MatrixVariable(value = "age",pathVar = "empId") Integer empAge){
        ......
    }

​ SpringBoot默认禁用了矩阵变量的功能。要想使用必须手动开启。

​ 开启方式:在配置类上实现 WebMvcConfigurer 接口,并重写 configurePathMatch 方法

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        // 不移除;后面的内容。矩阵变量功能就可以生效
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值