码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • axios和SpringMVC数据交互(一维二维数组,JSON/form形式,@RequestBody/@RequestParam)


    目录

    • 需求
    • 环境准备
      • 前端
      • 后端
    • 成功实现的案例
      • 以JSON形式发送double数组
      • 以JSON形式发送对象,对象中有数组
      • 以JSON形式发送对象,对象中有二维数组
      • 以x-www-form-urlencoded形式发送一维数组

    需求

    前端或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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    import request from 'src/utils/request'
    request({
            method: 'post',
            url: '/tests',
            headers: { 'Content-Type': 'application/json' },
            data: 要发送的数据,
          }).then((res) => {
            console.log(res) // 打印一下返回的结果
          })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    后端

    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)
          })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • postman(与上方的前端代码等效)
      和前端一个效果

    • 后端
      JSON要用@RequestBody去接收

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

    以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)
          })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • postman
      在这里插入图片描述

    • 后端

    public class TestEntity
    { // 记得补全getter setter toString
        List<Double> ld; // 注意@RequestBody不能自动映射大写字母开头的属性,这里都是小写,大写的需要@JsonProperty("XX")
        // double[] ld也是可以解析出来的
        String s;
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    @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()); // 一个字符串
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

    • 前端或postman
      data0或者postman框中的文本改为
    {
        "ld":[
            [1,2,3],
            [4,5,6],
            [7,8,9]
            ],
        "s":"一个字符串"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 后端
      把TestEntity的ld改成List>类型或者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()); // 一个字符串
    
    • 1
    • 2
    • 3
    • 4

    以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
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

  • 相关阅读:
    【跟乐乐学seata分布式事务组件】springCloudAlibaba分布式组件Seata 1.3.0集成教程
    JWT(2):JWT入门使用
    【C++】空间配置器 allocator:原理及底层解析
    酒店客房管理系统|基于Springboot的酒店客房管理系统设计与实现(源码+数据库+文档)
    【Web_接口测试_Python3_日期时间库】Arrow获取过去/当前未来时间日期、格式化时间日期、转换时间戳、获取不同时区时间日期等
    Redis微服务架构
    Elasticsearch (ES)内存管理降低内存占用率
    Jmeter的性能测试
    PostgreSQL对已有表增加自增序列
    【NIPS 2019】PVCNN:用于高效3D深度学习的点-体素 CNN
  • 原文地址:https://blog.csdn.net/qq_43140890/article/details/128198155
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号