javascript - 如何使用spring mvc接收数组作为参数?

标签 javascript java spring spring-mvc vue.js

使用 JSON.stringify 将数组对象转换为 json 字符串

var array = [1, 2];
let json = JSON.stringify(array);
console.log(json);
axios.get('http://localhost/goods', json).then(function (res) {
    if (res.code == 200) {
        console.log("ok");
    }
}

使用 Chrome 浏览器控制台传输时的参数:

enter image description here

我的 cargo Controller 类,例如:
@RequestMapping(value = "goods",method = RequestMethod.GET)
public String deleteByIds(@RequestBody Integer[] ids) {
    goodsService.deleteByIds(ids);
    return "ok";
}

Spring mvc 接收不到数组,还是我写 axios 代码有问题?如何解决?

最佳答案

根据您的要求,

axios.get('http://localhost/goods', json)

这是一个获取请求。所以它不会有 body 。

您可以尝试将 get 方法更改为发布或使用@RequestParameter 而不是@RequestBody。

关于javascript - 如何使用spring mvc接收数组作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52465200/

相关文章:

java - 转换 JDBC 连接

java - 谁能解释一下 "Loggers are stored in a namespace hierarchy "是什么意思?

spring - 使用 freemarker 上传文件时的 HTTP 状态 500

javascript - 错误 : Attempting to set key on an object that is immutable and has been frozen

javascript - Nodejs文件系统数组排序数字

javascript - 在iframe上检测鼠标移动?

java - 由于 java 中缺少证书(基于 XML SOAP)而导​​致使用 Web 服务时出错

java - 没有设置数据源就无法启动 spring boot 应用程序

java - 无法在 thymeleaf 中显示 byte[] 图像

javascript - 无法在angularjs中加载下拉列表的ng-model