http 协议支持多种参数提交方式,主要取决于请求方法(method)和内容类型(content-type)。
以下是主要的参数提交协议:
1. url 查询参数 (query parameters)
- 请求方法: get (也可用于其他方法)
- 格式:
?key1=value1&key2=value2 - 示例:
get /users?id=123&name=john - 获取方式:
- springboot:
@requestparam - servlet:
request.getparameter()
- springboot:
2. 表单提交 (form data)
- 请求方法: post (也可用于 put/patch)
- content-type:
application/x-www-form-urlencoded或multipart/form-data - 格式:
application/x-www-form-urlencoded:key1=value1&key2=value2(类似查询参数但在请求体中)multipart/form-data: 用于文件上传,包含边界分隔符
- 示例:
<form action="/submit" method="post" enctype="application/x-www-form-urlencoded"> <input name="username" value="john"> <input type="submit"> </form>
- 获取方式:
- springboot:
@requestparam或@modelattribute - servlet:
request.getparameter()
- springboot:
3. json 请求体 (json payload)
- 请求方法: post/put/patch/delete
- content-type:
application/json - 格式: json 对象
- 示例:
{
"name": "john",
"age": 30
}- 获取方式:
- springboot:
@requestbody - servlet: 通过
request.getreader()读取输入流
- springboot:
4. xml 请求体 (xml payload)
- 请求方法: post/put/patch/delete
- content-type:
application/xml或text/xml - 格式: xml 文档
- 示例:
<user> <name>john</name> <age>30</age> </user>
- 获取方式:
- springboot:
@requestbody配合 xml 解析器 - servlet: 通过
request.getreader()读取输入流
- springboot:
5. restful 路径参数 (path variables)
- 请求方法: 任意方法
- 格式:
/resource/{id} - 示例:
get /users/123 - 获取方式:
- springboot:
@pathvariable - servlet: 需要手动解析 url
6. http 头部参数 (headers)
- 位置: http 头部
- 示例:
authorization: bearer token123 accept-language: en-us
- 获取方式:
- springboot:
@requestheader - servlet:
request.getheader()
7. cookie 参数
- 位置: http cookie 头部
- 示例:
cookie: sessionid=abc123; username=john
- 获取方式:
- springboot:
@cookievalue - servlet:
request.getcookies()
8. graphql 查询
- 请求方法: post/get
- content-type:
application/json(通常) - 格式: graphql 查询语言
- 示例:
{
"query": "{ user(id: 123) { name age } }"
}- 获取方式: 需要专门的 graphql 处理器
9. websocket 参数
- 协议: websocket
- 格式: 自定义,通常为 json 或二进制
- 获取方式: 通过 websocket api 处理
10. server-sent events (sse)
- 协议: http 长连接
- content-type:
text/event-stream - 格式: 特定的事件流格式
- 获取方式: 通过专门的 sse 客户端处理
总结表
| 提交方式 | 典型 content-type | 主要用途 | springboot 注解 |
|---|---|---|---|
| url 查询参数 | - | get 请求简单参数 | @requestparam |
| 表单数据 | application/x-www-form-urlencoded | 传统表单提交 | @requestparam, @modelattribute |
| 多部分表单 | multipart/form-data | 文件上传 | @requestpart |
| json 请求体 | application/json | rest api 复杂数据 | @requestbody |
| xml 请求体 | application/xml | 传统 xml 接口 | @requestbody |
| 路径参数 | - | restful url 设计 | @pathvariable |
| http 头部 | - | 认证、语言偏好等 | @requestheader |
| cookie | - | 会话管理、用户追踪 | @cookievalue |
到此这篇关于http 与 springboot 参数提交与接收协议方式的文章就介绍到这了,更多相关http与springboot参数提交内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论