SpringMVC不同类型的请求参数接收方式

本文详细介绍了SpringMVC中处理器方法如何接收不同类型请求参数,包括普通数据类型、POJO、数组和集合。通过@RequestParam、@PathVariable注解实现不同命名参数的绑定,同时展示了GET和POST请求中如何传递和接收POJO类型的参数,以及数组和集合类型的参数。此外,还提到了@RequestParamList用于处理多个相同名称的请求参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请求

请求参数

处理器方法怎么接收外面传递过来的参数?

SpringMVC将传递过来的参数封装到处理器方法的形参里面。

请求参数类型

1.普通数据类型参数

2.POJO类型参数

3.数组类型参数

4.集合类型参数

实际项目开发中,一般都会封装为实体类的形式接收参数接收

1.普通类型参数接收方式
1.1单个参数

java的controller控制器:

	 //http://localhost:8080/user/requestParam1?name=zhangsan
	@ResponseBody
    @RequestMapping("/requestParam1")
    public void requestParam1(String name) {
        System.out.println(name);
    }

postman请求方式:
在这里插入图片描述

1.1.1单个URL上的路径参数

java的controller控制器:

	 //http://localhost:8080/user/requestParam11/zhangsan
    @ResponseBody
    @RequestMapping("/requestParam11/{name}")
    public void requestParam11(@PathVariable("name") String name) {
        System.out.println(name);
    }

postman请求方式:
在这里插入图片描述

1.2多个参数

java的controller控制器:

	//http://localhost:8080/user/requestParam2?name=zhangsan&age=23
    @ResponseBody
    @RequestMapping("/requestParam2")
    public void requestParam2(String name,String age) {
        System.out.println(name);
        System.out.println(age);
    }

postman请求方式:
在这里插入图片描述

1.2.1多个URL上的路径参数

java的controller控制器:

	//http://localhost:8080/user/requestParam21/zhangsan/23
    @ResponseBody
    @RequestMapping("/requestParam21/{name}/{age}")
    public void requestParam21(@PathVariable("name") String name,
                               @PathVariable("age") String age) {
        System.out.println(name);
        System.out.println(age);
    }

postman请求方式:
在这里插入图片描述

2.如果参数名不一致如何传参

使用@RequestParam注解

名称@RequestParam
类型形参注解
位置处理器类中的方法形参前
作用用来绑定请求参数与对应方法形参之间的关系
2.1参数不一致传参

java的controller控制器:

	 //http://localhost:8080/user/requestParam31?userName=zhangsan
    @ResponseBody
    @RequestMapping("/requestParam31")
    public void requestParam31(@RequestParam("userName") String name) {
        System.out.println(name);
    }

postman请求方式:
在这里插入图片描述

2.POJO类型参数接收
2.1Get请求(一般不这么写)

java的controller控制器:

	//http://localhost:8080/user/requestParam41?id=1&name=zhangsan&age=23
	@ResponseBody
    @RequestMapping("/requestParam41")
    public void requestParam41(User user) {
        System.out.println(user);
    }

postman请求方式:
在这里插入图片描述

2.2post请求:form表单post提交

java的controller控制器:

 	//http://localhost:8080/user/requestParam41
    @ResponseBody
    @RequestMapping("/requestParam41")
    public void requestParam41(User user) {
        System.out.println(user);
    }

postman请求方式:
在这里插入图片描述

2.3post请求:Json

java的controller控制器:

 	//http://localhost:8080/user/requestParam41
    @ResponseBody
    @RequestMapping("/requestParam41")
    public void requestParam41(@RequestBody User user) {
        System.out.println(user);
    }

postman请求方式:
在这里插入图片描述

3.数组类型参数接收

java的controller控制器:

  	//http://localhost:8080/user/requestParam51?name=zhangsan&name=lisi&name=wangwu
    @ResponseBody
    @RequestMapping("/requestParam51")
    public void requestParam51(String[] name) {
        System.out.println(name[0]+","+name[1]+","+name[2]);
    }

postman请求方式:
在这里插入图片描述

4.集合类型参数接收

java的controller控制器:

 	//http://localhost:8080/user/requestParam61?nick=nick1&nick=nick2&nick=nick3
    @ResponseBody
    @RequestMapping("/requestParam61")
    public void requestParam61(@RequestParam List<String> nick) {
        System.out.println(nick);
    }

postman请求方式:
在这里插入图片描述

注意:SpringMVC默认将List作为对象处理,赋值前先创建对象,然后将形参作为对象的属性进行处理。由于List是接口,无法创建对象,报无法找到构造方法异常;修复类型为可创建对象的ArrayList类型后,对象可以创建,但是没有形参对应的属性,因此数据为空。此时需要告知SpringMVC的处理器形参是一组数据,而不是一个单一数据。通过@RequestParam注解,将数量大于1个的参数打包成参数数组后,SpringMVC才能识别该数据格式,并判定形参类型是否为数组或集合,并按数组或集合对象的形式操作数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值