栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Axios请求以及对应的Springboot接收参数

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Axios请求以及对应的Springboot接收参数

课题来源

前一段时间做项目的时候前后端参数的对接出现了很大问题,导致痛不欲生,故想把一直没有搞明白的前后端传参接收参数的方法搞清楚,记录以下内容帮助我记忆,以及给需要的人一点帮助。

详细方法和代码 1、Axios的GET请求方式 问号传参
  • 前端代码
axios.get("http://localhost:8081/boot/one?name=xxx&age=22")
  .then(function (res) {
    console.log(res)
  })
  .catch(function (err) {
    console.log(err)
  })
  • 后端接收
@GetMapping(value = "/boot/get1")
public Result getParamByQ(String name, Integer age) {
    if (name != null && age != null) {
        return new Result(name + age, "请求成功", 100);
    }
    return new Result(null, "请求失败", -1);
}
反斜杠传参
  • 前端代码
axios.get("http://localhost:8081/boot/get2/xxx/22")
  .then(function (res) {
    console.log(res)
  })
  .catch(function (err) {
    console.log(err)
  })
  • 后端代码
@GetMapping(value = "/boot/get2/{name}/{age}")
public Result getParamByX(@PathVariable String name, @PathVariable Integer age) {
    if (name != null && age != null) {
        return new Result(name + age, "请求成功", 100);
    }
    return new Result(null, "请求失败", -1);
}
2、Axios的POST请求方式 JSON方式
  • 前端代码
axios.post('http://localhost:8081/user/login', {
  uid: that.username,
  pwd: that.password
}).then(function (res) {
  console.log(res)
}).catch(function (err) {
  console.log(err)
})
  • 后端代码
@PostMapping(value = "/user/login")
public Result Login(@RequestBody User loginParam) {
    User user = userService.getUserById(loginParam);
    if (user != null) {
        String token = UUID.randomUUID() + "";
        // 存储token到redis
        redisTemplate.opsForValue().set(token, user, Duration.ofMinutes(30l));
        return new Result(token, "登录成功", 100);
    }
    return new Result(null, "登录失败", -1);
}

此处的User类有两个类成员变量分别为Long uid;String pwd;

x-www-form-urlencoded方式
  • 前端代码
    需要引入Qs模块,前端控制台执行npm install Qs
axios.post("http://localhost:8081/boot/post1", Qs.stringify({name: 'hqz', age: 22}), {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
}).then(function (res) {
  console.log(res)
}).catch(function (err) {
  console.log(err)
})
  • 后端代码
@PostMapping(value = "/boot/post1")
public Result getDataByForm(@RequestParam(value = "name") String name, @RequestParam(value = "age") Integer age) {
    if (name != null && age != null) {
        return new Result(name + age, "请求成功", 100);
    }
    return new Result(null, "请求失败", -1);
}
FormData方式
  • 前端代码
    前端需要构造一个FormData类
let fd = new FormData()
fd.append("name", "xxx")
fd.append("age", 22)

axios.post("http://localhost:8081/boot/post2", fd, {
  headers: {'Content-Type': 'multipart/form-data'}
}).then(function (res) {
  console.log(res)
}).catch(function (err) {
  console.log(err)
})
  • 后端代码
@PostMapping(value = "/boot/post2")
public Result getDataByFormData(@RequestParam(value = "name") String name, @RequestParam(value = "age") Integer age) {
    if (name != null && age != null) {
        return new Result(name + age, "请求成功", 100);
    }
    return new Result(null, "请求失败", -1);
}
3、Axios文件上传 FormData方式
  • 前端代码
let fd = new FormData()
fd.append("file", this.file)
axios.post("http://localhost:8081/boot/postFile", fd, {
  headers: {'Content-Type': 'multipart/form-data'}
}).then(function (res) {
  console.log(res)
}).catch(function (err) {
  console.log(err)
})
  • 后端代码
    后端可以接收图片文件,txt文件,视频文件等。但是后端接收文件的大小初始限制为1M,想要接收更大文件需要需改application.yml或其他配置文件。
spring:
  servlet:
    multipart:
      max-file-size: 80000000 # 单位为byte
@PostMapping(value = "/boot/postFile")
public Result getFile(@RequestParam(value = "file") MultipartFile multipartFile) throws IOException {
    if (multipartFile != null){
        String originName = multipartFile.getOriginalFilename();
        String folder = "D:\";
        String picName = new Date().getTime() + originName;
        File fileLocal = new File(folder, picName);
        multipartFile.transferTo(fileLocal);
        return new Result("文件收到", "post成功", 100);
    }
    return new Result(null, "post失败", -1);
}
参考文献
  1. 上传文件过大时,报The field file exceeds its maximum permitted size of 1048576 bytes问题
  2. axios post 请求类型和 SpringBoot 参数接收知识
  3. @RequestBody的使用
  4. 关于接收x-www-form-urlencoded格式数据的后端
  5. springboot 接收文件
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/978730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号