前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot使用矩阵传参

SpringBoot使用矩阵传参

作者头像
半月无霜
发布2023-03-03 14:44:21
3000
发布2023-03-03 14:44:21
举报
文章被收录于专栏:半月无霜半月无霜

SpringBoot使用矩阵传参

一、介绍

在平时,我们在进行请求接口时,我们一个请求url的样子往往是下面这样子的

代码语言:javascript
复制
http://localhost:8080/user/get?name=半月&age=18

对于上面的请求url,我们只需要使用@RequestParam注解就可以将其获取,十分简单。

那么,对于下面的这个矩阵传参的url,我们该如何进行获取呢?

代码语言:javascript
复制
http://localhost:8080/user/get;name=半月;age=18

http://localhost:8080/user/delete;id=11,12,13;status=1

这时候,我们就该使用到@MatrixVariable这个注解了,具体使用如下。

二、使用

1)基本使用

springBoot中,默认是去掉了url分号后的内容。如此一来,我们在使用矩阵传参时,需要对其进行开启。

代码语言:javascript
复制
package com.banmoon.test.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);// 默认为true,这里设置为不移除分号后面的内容
        configurer.setUrlPathHelper(urlPathHelper);
    }
}

或者可以这样写,效果都是一样的,熟悉@bean配置的大家都知道。推荐使用上面这种,简单明了

代码语言:javascript
复制
package com.banmoon.test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

@Configuration
public class MyBeanConfig {

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                urlPathHelper.setRemoveSemicolonContent(false);// 默认为true,这里设置为不移除分号后面的内容
                configurer.setUrlPathHelper(urlPathHelper);
            }
        };
        return webMvcConfigurer;
    }
}

对于下面这两个url请求,我们可以这样写接口

代码语言:javascript
复制
http://localhost:8080/user/get/get;name=半月;age=18

http://localhost:8080/user/delete/delete;name=半月;age=18,19,20
代码语言:javascript
复制
package com.banmoon.test.controller;

import com.banmoon.test.dto.ResultData;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserMatrixVariableController {

    @GetMapping("/get/{path}")
    public ResultData get(@MatrixVariable String name,
                          @MatrixVariable String age,
                          @PathVariable String path){
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("age", age);
        map.put("path", path);
        return ResultData.success(map);
    }

}

请求查看结果

image-20220606141224478
image-20220606141224478
image-20220606141446357
image-20220606141446357

2)其他

另外一种请求url,看着很乱

代码语言:javascript
复制
http://localhost:8080/user/setUserPost/101;status=1/post/111;status=1

接口是这样的

代码语言:javascript
复制
package com.banmoon.test.controller;

import com.banmoon.test.dto.ResultData;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserMatrixVariableController {

    @GetMapping("/setUserPost/{userId}/post/{postId}")
    public ResultData setUserPost(@PathVariable Integer userId,
                                  @PathVariable Integer postId,
                                  @MatrixVariable(value = "status", pathVar = "userId") Integer userStatus,
                                  @MatrixVariable(value = "status", pathVar = "postId") Integer postStatus){
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("postId", postId);
        map.put("userStatus", userStatus);
        map.put("postStatus", postStatus);
        return ResultData.success();
    }

}
image-20220606144809442
image-20220606144809442

三、最后

在处理完矩阵传参后,我就知道这玩意有多么不受待见了,我也搞不懂会有什么样的业务场景去使用这种传参模式。

好吧,可以不用,但不能不知道。

我是半月,祝你幸福!!!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot使用矩阵传参
    • 一、介绍
      • 二、使用
        • 1)基本使用
        • 2)其他
      • 三、最后
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档