【SpringMVC应用篇】SpringMVC请求参数接收处理


前端向后台传递参数主要分两类:Get方式的URL路径传参和Post方式的Body请求体传参

1、URL传参数:/getParam?age=1&username="封于修"
2、Post方式的Body请求体传参: contextType = “application/x-www-form-urlencoded” contextType = “application/json”

get和post请求的差异

@GetMapping注解标记的Handle,SpringMVC参数处理只能解析Get请求中的key=value对,无法解析请求体body区域的application/x-www-form-urlencoded和application/json格式的数据
在这里插入图片描述
@PostMapping注解标记的Handle,能解析Get请求中的key=value对和解析请求体body区域的application/x-www-form-urlencoded(from表单提交的数据 key1=val1&key2=val2 )格式数据,如果两者都存在参数相同的数据,则都解析。
测试代码:controller
在这里插入图片描述
接口测试:
在这里插入图片描述
响应结果:
在这里插入图片描述

如果需要支持解析application/json格式的数据,需要使用注解@RequestBody的支持,告诉SpringMVC使用RequestResponseBodyMethodProcessor处理器解析器解析
在这里插入图片描述

基本数据类型传参 (8种基本数据类型)

前端传参:

/getParam?a1=1&a2=2&a3=3&a4=4&a5=5&a6=6&a7=0&a8=a

后端接收入参:

@GetMapping("/getParam")
public R testGetParam(byte a1, short a2, int a3, long a4,
                      double a5, float a6,
                      boolean a7,
                      char a8) {
    System.out.println("a1: "+ a1);
    System.out.println("a2: "+ a2);
    System.out.println("a3: "+ a3);
    System.out.println("a4: "+ a4);
    System.out.println("a5: "+ a5);
    System.out.println("a6: "+ a6);
    System.out.println("a7: "+ a7);
    System.out.println("a8: "+ a8);
    return success(null);
}

建议都使用对应的包装类型,前端未传入的值包装类型默认为null。基本数据类型前端未传入值,后端报错
在这里插入图片描述

引用数据类型传参 (数组,String)

前端传参:

url参数: /postParam?age=1,2,3
请求体参数: body = “username=步惊云,风雨”

在这里插入图片描述

后端接收入参:

@PostMapping("/postParam")
public R testPostParam(Integer[] age, String[] username) {
    System.out.println("age: "+ Arrays.toString(age));
    System.out.println(Arrays.toString(username));
    return success(null);
}

复杂对象参数

@RequestParam和@RequestBody的区别

1、@RequestParam可以取get方式的url路径参数和post方式的body类型为contextType = "application/x-www-form-urlencoded"的参数,当方法参数为基本数据类型时,前端未传值,可以@RequestParam给方法参数设置默认值

2、@RequestBody只能取post方式的body类型为contextType = "application/json"的参数

3、@RequestBody 无法直接解析基本数据类和对应的包装类型,能解析对象类型: String、JavaBean、List、Set、Map

application/x-www-form-urlencoded

单个参数

单个参数,可以使用@RequestParam注解或者不使用。形参名必须和后端保持一致,否则无法获取。
在这里插入图片描述

    @PostMapping("/saveSingle")
    public ResultVO saveSingle(String xss, String sql) {
        System.out.println(JSONObject.toJSONString(xss));
        System.out.println(JSONObject.toJSONString(sql));
        return ResultVO.ok(JSONObject.toJSONString(new User(xss,sql)));
    }

多个参数

Controller层属性单个属性接收,如果太多,我们的形参代码就会很长,所以可以使用实体类/Map来接收复杂的参数

1、Map接收,需要使用@RequestParam注解,否则无法获取值
在这里插入图片描述

@PostMapping("/saveMap")
public ResultVO save(@RequestParam Map map) {
    System.out.println(JSONObject.toJSONString(map));
    return ResultVO.ok(JSONObject.toJSONString(map));
}

2、实体接收,不需要使用@RequestParam注解,否则报错。参数必须和实体属性一致,否则无法获取。
@RequestParam注解报错信息
在这里插入图片描述
在这里插入图片描述

@PostMapping("/saveEntity")
public ResultVO saveUser(User user) {
    System.out.println(JSONObject.toJSONString(user));
    return ResultVO.ok(JSONObject.toJSONString(user));
}

3、多个实体接收,不需要使用@RequestParam注解,否则报错。参数必须和实体属性一致,否则无法获取。

在这里插入图片描述

@PostMapping("/saveEntity")
public ResponseEntity<List> saveUser(User user, Person person) {
    System.out.println(user);
    System.out.println(person);
    ArrayList<Object> list = new ArrayList<>();
    list.add(user);
    list.add(person);
    return ResponseEntity.ok(list);
}

4、多个实体接收,不需要使用@RequestParam注解,否则报错。参数必须和实体属性一致,否则无法获取。可以实现一个对象接收get参数,一个接收application/json类型参数

@PostMapping("/saveEntity")
public ResponseEntity<List> saveUser(User user, @RequestBody Person person) {
    System.out.println(user);
    System.out.println(person);
    ArrayList<Object> list = new ArrayList<>();
    list.add(user);
    list.add(person);
    return ResponseEntity.ok(list);
}

application/json

前端传JSON数组, 后端List接收

在这里插入图片描述
后端接收入参:

@PostMapping("/postJsonListParam")
public R postJsonListParam(@RequestBody List<Map> map) {
    return success(map);
}

前端传JSON 对象, 后端Map接收

在这里插入图片描述
后端接收入参:

@PostMapping("/postJsonMapParam")
public R postJsonMapParam(@RequestBody Map map) {
   return success(map);
}

前端传JSON 对象, 后端实体接收

在这里插入图片描述
后端接收入参:

@PostMapping("/postJsonUserParam")
public R postJsonMapParam(@RequestBody User user) {
   return success(user);
}

项目开发建议使用(@RequestBody 实体)接收,偷懒的情况可以使用(@RequestBody Map),Map接收一切对象,解决任何复杂对象的接收

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

李少谦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值