spring boot 请求参数接收控制指南
1. 概述
在spring boot项目中,我们可以通过不同的注解来接收来自http请求的各种参数。
本指南将详细介绍各种参数接收方式及其使用场景。
2. 有注解时参数接收方式对比
参数类型 | 注解 | 位置 | 适用http方法 | 示例 |
---|---|---|---|---|
路径参数 | @pathvariable | url路径中 | get, delete | /users/{id} |
查询参数 | @requestparam | url?后 | get | /users?name=john |
请求体 | @requestbody | 请求体 | post, put, patch | json/xml数据 |
请求头 | @requestheader | http头 | 任意 | authorization: bearer token |
cookie值 | @cookievalue | cookie头 | 任意 | cookie: jsessionid=xxx |
表单数据 | @requestparam | 表单体 | post | application/x-www-form-urlencoded |
文件上传 | @requestparam | multipart/form-data | post | 文件上传表单 |
3. 无注解时接收参数默认位置
spring boot 默认参数绑定规则(不加注解时)
请求类型 | 参数类型 | 默认绑定位置 | 示例 | 注意事项 |
---|---|---|---|---|
get | 基本类型/string | 自动绑定到url查询参数 | get /user?name=tom → public string getuser(string name) | 参数名必须与url中的key一致 |
pojo对象 | 自动绑定到url查询参数 | get /user?name=tom&age=20 → public string getuser(user user) | 对象的字段名需与url参数匹配 | |
数组/集合 | 自动绑定到url查询参数 | get /user?ids=1,2,3 → public string getuser(list<integer> ids) | 支持逗号分隔或同名参数(ids=1&ids=2) | |
post | 基本类型/string | 表单数据 (form data) | post /user (content-type: application/x-www-form-urlencoded) | 需设置content-type为表单类型 |
pojo对象 | 表单数据或json请求体 | 若为application/json → 绑定到请求体;若为表单 → 按字段名匹配 | 需明确指定content-type,否则可能解析失败 | |
数组/集合 | 表单数据或json请求体 | 同pojo规则 | 表单格式需用同名参数(names=tom&names=jerry) | |
multipartfile | 文件上传 (multipart) | post /upload (enctype="multipart/form-data") | 必须设置enctype="multipart/form-data" | |
put/patch | 所有类型 | json/xml请求体 | put /user { "name": "tom" } → public string updateuser(user user) | 默认按@requestbody处理,需显式加注解才能覆盖 |
delete | 基本类型/string | url查询参数 | delete /user?id=123 → public string deleteuser(long id) | 与get相同 |
pojo对象 | 不支持 | - | delete通常只用路径参数或简单查询参数 |
关键规则总结
get/delete 请求:
- 参数默认绑定到 url查询字符串(
?key=value
)。 - pojo对象会按字段名自动拆解到查询参数。
post 请求:
- 默认按 表单数据(
application/x-www-form-urlencoded
)解析。 - 若请求头为
content-type: application/json
,则需显式加@requestbody
,否则绑定失败。
put/patch 请求:
- 默认按请求体(json/xml)处理,类似
@requestbody
行为,但实际开发中建议显式加注解。
特殊类型:
multipartfile
:仅在multipart/form-data
类型的post中自动绑定。- 数组/集合:支持url查询参数(get)或表单同名参数(post)。
常见误区澄清
表单数据不会放在请求头
- 请求头仅用于元数据(如
content-type
、authorization
等),表单内容一定在请求体。
get请求能否带表单数据?
- ❌ 不能。get请求的参数只能通过url查询字符串(
?key=value
)传递,且长度受限。
不加注解时spring boot如何绑定表单数据?
- 默认按参数名匹配请求体中的表单字段(需参数名与表单字段名一致)。
post json vs 表单数据
- json数据必须显式使用
@requestbody
,而表单数据默认绑定(无需注解)。
4.http请求参数位置自定义控制对照表
参数位置 | spring boot注解 | 手动提取方式 | 适用场景 | 示例请求 |
---|---|---|---|---|
url路径参数 | @pathvariable | httpservletrequest.getrequesturi() | restful资源标识 | get /users/123 → id=123 |
url查询参数 | @requestparam | request.getparameter("name") | 过滤条件、分页 | get /users?name=tom → name=tom |
请求头 | @requestheader | request.getheader("authorization") | 认证令牌、客户端信息 | header: authorization: bearer xxx |
请求体(json) | @requestbody | inputstream + json解析库 | 复杂数据提交 | post /users + {"name":"tom"} |
请求体(表单) | @requestparam | request.getparametermap() | 传统表单提交 | post /login + username=tom&password=123 |
请求体(文件) | @requestpart | multiparthttpservletrequest | 文件上传 | post /upload + multipart/form-data |
cookie | @cookievalue | request.getcookies() | 会话管理 | cookie: sessionid=abc123 |
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论