半月无霜の博客

SpringBoot使用矩阵传参

Toretto·2022-06-24 19:36·149 次阅读

SpringBoot使用矩阵传参

SpringBoot使用矩阵传参

一、介绍#

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

Copy
http://localhost:8080/user/get?name=半月&age=18

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

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

Copy
http://localhost:8080/user/get;name=半月;age=18
http://localhost:8080/user/delete;id=11,12,13;status=1

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

二、使用#

1)基本使用#

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

Copy
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配置的大家都知道。推荐使用上面这种,简单明了

Copy
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请求,我们可以这样写接口

Copy
http://localhost:8080/user/get/get;name=半月;age=18
http://localhost:8080/user/delete/delete;name=半月;age=18,19,20
Copy
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-20220606141446357

2)其他#

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

Copy
http://localhost:8080/user/setUserPost/101;status=1/post/111;status=1

接口是这样的

Copy
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

三、最后#

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

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

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

posted @   半月无霜  阅读(149)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示
目录