SpringMVC中使用@PathVariable绑定路由中的数组的方法
作者:潘佳琦 时间:2023-11-27 14:21:01
问题描述
在开发批量删除功能时,往往都是多条数据,所以前台需要传一个数组给后台,但是怎么在URL
中绑定一个数组,同时在后台用@PathVariable
来绑定呢?
原来的做法
后台:
前台:
以上是之前项目的截图,直接拿过来啦!!!😁😁😁
后台以@RequestBody接收一个数组,但是前台需要发送一个request请求, 传入请求方法的类型delete, 并且传入一个body, 但在写华软项目时, 张喜硕组长看到,说这样写并不好,要在url中绑定一个数组,在前台以逗号隔开。delete方法是不建议传输实体的主体
delete请求用于请求服务器删除所请求URI(统一资源标识符,Uniform Resource Identifier)所标识的资源。DELETE请求后指定资源会被删除
该如何解决呢?如何在url
绑定数组呢?
解决方法
后台:
@DeleteMapping("{clusterNodeId}/hosts/{hostsId}")
public void deleteClusterNodeByHostList(@PathVariable Long clusterNodeId, @PathVariable Long[] hostsId) {
clusterNodeService.deleteClusterNodeByHostList(clusterNodeId, hostsId);
}
由url
接收一个数组,使用@PathVariable
绑定这个数组
前台:
public deleteClusterNodeByHostList(
clusterNodeId: number,
hostsId: any[]): Observable<ClusterNode> {
return this.http.delete(`${this.baseUrl}/${clusterNodeId.toString()}/hosts/${hostsId.join(',')}`);
}
使用join 对数组进行处理,以逗号的形式隔开,传给后台
url:
后台接收:
来源:https://segmentfault.com/a/1190000019741292
标签:SpringMVC,@PathVariable,绑定,数组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈关于spring profile的误解
2021-07-25 05:48:43
java常用工具类之DES和Base64加密解密类
2023-12-20 17:48:31
基于Android XML解析与保存的实现
2023-05-25 18:54:32
简单的观察者模式示例分享
2023-02-11 12:52:12
Kotlin基础教程之Run,标签Label,函数Function-Type
2022-08-28 14:11:01
![](https://img.aspxhome.com/file/2023/6/85406_0s.png)
IDEA将Maven项目中指定文件夹下的xml等文件编译进classes的方法
2023-05-07 07:34:50
![](https://img.aspxhome.com/file/2023/6/122286_0s.png)
C# javascript 读写Cookie的方法
2023-03-19 08:03:16
Android实现控制摄像头拍照
2022-06-03 02:14:06
![](https://img.aspxhome.com/file/2023/1/126781_0s.jpg)
Java Selenium实现多窗口切换的示例代码
2022-01-22 22:48:08
IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 创建多模块项目的超详细教程
2021-11-08 00:42:36
![](https://img.aspxhome.com/file/2023/8/121928_0s.png)
使用Java的Lucene搜索工具对检索结果进行分组和分页
2022-07-27 05:21:17
![](https://img.aspxhome.com/file/2023/9/70929_0s.png)
Java volatile如何实现禁止指令重排
2022-01-13 18:29:24
![](https://img.aspxhome.com/file/2023/8/95948_0s.png)
photoView实现图片多点触控效果
2023-06-21 20:16:03
![](https://img.aspxhome.com/file/2023/7/122057_0s.jpg)
Java使用openssl检测网站是否支持ocsp
2022-10-03 15:55:55
![](https://img.aspxhome.com/file/2023/2/113502_0s.jpg)
宝塔面板配置及部署javaweb教程(全网最全)
2023-11-10 15:26:27
![](https://img.aspxhome.com/file/2023/5/58905_0s.jpg)
Java 动态代理的多种实现方式
2023-11-20 05:23:14
![](https://img.aspxhome.com/file/2023/6/91126_0s.png)
Java数组队列概念与用法实例分析
2023-11-18 04:18:31
![](https://img.aspxhome.com/file/2023/8/90038_0s.png)
关于Flyweight模式应用实践的相关介绍
2021-07-28 21:50:34
c# 给pdf添加数字签名的步骤
2022-04-05 17:58:01
![](https://img.aspxhome.com/file/2023/1/71741_0s.png)
MyBatis-Plus代码生成器的使用详解
2022-09-04 23:14:40
![](https://img.aspxhome.com/file/2023/4/85824_0s.png)