Controller中接收数组参数 post请求中在body中传+post请求中通过表单形式传(x-www-form-urlencoded)

原文网址:Controller中接收数组参数 post请求中在body中传+post请求中通过表单形式传(x-www-form-urlencoded)

1、场景
需要根据用户id集合批量删除用户数据,前端使用post请求,controller中参数接收数组参数并根据用户id删除用户基本信息

2、分析处理:
2.1、前端请求类型contentType:application/json
请求体中为json字符串,后端新建一个UserDTO(用户数据传输类),用于接收前端json参数。

    在controller参数体中直接使用@RequestBody UserDTO userDTO即可将前端数组自动转化为userDTO中对应的属性

前端请求及后端返回值:
在这里插入图片描述
controller
在这里插入图片描述
userDTO中需要包含name、ids属性,如下:
在这里插入图片描述
2.2、前端请求类型contentType:application/x-www-form-urlencoded(form表单提交)
在这里插入图片描述
需要注意的是,上图中不是选择的form-data (这是get请求通过表单形式提交应该选择的)
controller
在这里插入图片描述

实例代码:

** 实战 :后台使用:@RequestParam(“userId”) Long userId 这种的,前台该怎么传参**


后端代码:

   //获取用户信息 通过id
    @GetMapping("/getUserById")
    @Operation(method = "GET", summary = "根据id查询用户信息")
    public Result<UserResVO> getUserInfoById(@RequestParam("userId") Long userId) {
        if (ObjectUtil.isEmpty(userId)) {
            return Result.fail(Constant.ErrorCode, "用户id不能为空");
        }
        return userService.getUserInfoById(userId);
    }

Vue
前段js代码-定义方法
// 结算单详情
export function getUserInfoById(id) {
  return request({
    url: '/User/getUserById',
    method: 'get',
    params: {userId: id},
  });
}


//前端vue中的写法--引用方法
import {getUserInfoById} from  user.js

//使用:
function getUserDetail() {
  getUserInfoById(detailId.value).then((res) => {
    detailOb.value = res.data;
    。。。。。。
	}

实战:后台使用:@PathVariable(name=“userId”) Long userId 这种的,前台该怎么传参
在这里插入图片描述
参考:
Controller中接收数组参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执键行天涯

码你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值