axios和SpringMVC数据交互(一维二维数组,JSON/form形式,@RequestBody/@RequestParam)

需求

前端或postman发送数组,后端controller做为入参接收

环境准备

前端

// src/utils/request.js
const axiosInstance = axios.create({
  baseURL: 'http://localhost:8081/',
  // baseURL: 'http://106.14.92.82:8081/',
  timeout: 10000
})
export default axiosInstance
import request from 'src/utils/request'
request({
        method: 'post',
        url: '/tests',
        headers: { 'Content-Type': 'application/json' },
        data: 要发送的数据,
      }).then((res) => {
        console.log(res) // 打印一下返回的结果
      })

后端

springboot spingmvc

成功实现的案例

以JSON形式发送double数组

不是k-v,直接是一个数值数组

  • 前端
request({
        method: 'post',
        url: '/tests',
        headers: { 'Content-Type': 'application/json' },
        data: JSON.stringify([1,2,3]),
      }).then((res) => {
        console.log(res)
      })
  • postman(与上方的前端代码等效)
    和前端一个效果

  • 后端
    JSON要用@RequestBody去接收

    @PostMapping(value = "/tests")
    public void test(@RequestBody List<Double> BS){
        log.info(BS); // 打印 [1.0, 2.0, 3.0]
    }
    @PostMapping(value = "/tests")
    public void locateByFang(@RequestBody String BS){
        log.info(BS); // [1,2,3]      这是个长度为7的String
    }

以JSON形式发送对象,对象中有数组

  • 前端
const data0 = {
        'ld': [1,2,3],
        's':'一个字符串'
      }
request({
        method: 'post',
        url: '/tests',
        headers: { 'Content-Type': 'application/json' },
        data: data0
      }).then((res) => {
        console.log(res)
      })
  • postman
    在这里插入图片描述

  • 后端

public class TestEntity
{ // 记得补全getter setter toString
    List<Double> ld; // 注意@RequestBody不能自动映射大写字母开头的属性,这里都是小写,大写的需要@JsonProperty("XX")
    // double[] ld也是可以解析出来的
    String s;
 }
@PostMapping(value = "/tests")
    public void test(@RequestBody TestEntity BS){
        log.info(BS); // testEntity{ld=[1.0, 2.0, 3.0], s='一个字符串'}
        log.info(BS.getLd().size()); // 3
        log.info(BS.getS()); // 一个字符串
    }

以JSON形式发送对象,对象中有二维数组

  • 前端或postman
    data0或者postman框中的文本改为
{
    "ld":[
        [1,2,3],
        [4,5,6],
        [7,8,9]
        ],
    "s":"一个字符串"
}
  • 后端
    把TestEntity的ld改成List<List<Double>>类型或者double[][]类型
log.info(BS); // testEntity{ld=[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], s='一个字符串'}
log.info(BS.getLd().size());// 3
log.info(BS.getLd().get(0).size()); // 3
log.info(BS.getS()); // 一个字符串

以x-www-form-urlencoded形式发送一维数组

  • 前端
let oneDimArr = [1,2,3]
const usp = new URLSearchParams();
usp.append('BS', oneDimArr);
request({
  method: 'post',
  url: '/tests',
  headers: {'Content-Type': 'application/x-www-form-urlencoded'},
  data: usp
})
  • postman
    在这里插入图片描述

  • 后端

@PostMapping(value = "/tests")
public void locateByFang(@RequestParam(value="BS") double[] BS){
	for(double s : BS) log.info(s);
}
// 或者
@PostMapping(value = "/tests")
public void locateByFang(@RequestParam(value="BS") List<Double> BS){
	log.info(BS);
}

暂时没有实现使用x-www-form-urlencoded传输二维数组,3×3的数组到了后端变成了9×1的数组

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值