当前位置:网站首页>简述@RequestParam与@RequestBody参数注解
简述@RequestParam与@RequestBody参数注解
2022-08-04 20:29:00 【不知努力的bd】
一、@RequestParam
@RequestParam用于将指定的请求参数赋值给方法中的形参,可以接受简单类型属性,也可以接受对象类型,一般用于GET请求。
@RequestParam三个配置参数
required 表示是否必须,默认为 true,必填。
defaultValue 可设置请求参数的默认值。
value 为接收url的参数名(相当于key值)。
@RequestParam用来处理 Content-Type 为 application/x-www-form-urlencoded 编码的内容,Content-Type默认为该属性。
@RequestParam也可用于除GET请求外,其它类型的请求,例如:POST、DELETE等请求。
不推荐使用@RequestParam接收application/json,这时候就需要使用到@RequestBody。
举例:根据上面的这个URL,你可以用这样的方式来进行获取
http://localhost:8080/test/hello/0726?param1=10¶m2=20
//required不写时,默认为true
public String test(
@RequestParam(value="param1", required=true) String param1,
@RequestParam(value="param2", required=false) String param2){
...
}
@RequestParam 和 @PathVariable 注解是用于从request中接收请求的,两个都可以接收参数,关键点不同的是@RequestParam 是从request里面拿取值,而 @PathVariable 是从一个URI模板里面来填充
二、@RequestBody
@RequestBody一般用于POST请求,把参数放在requestbody里面。
GET请求没有HttpEntity,所以@RequestBody不适用。
POST请求中,通过HttpEntity传递的参数,必须要在请求头中声明数据的类型Content-Type,SpringMVC通过使用HandlerAdapter 配置的HttpMessageConverters来解析HttpEntity中的数据,然后绑定到相应的bean上。
@RequestBody接收的参数是来自requestBody中,即请求体。一般用于处理非 Content-Type: application/x-www-form-urlencoded编码格式的数据,比如:application/json、application/xml等类型的数据。
就application/json类型的数据而言,使用注解@RequestBody可以将body里面所有的json数据传到后端,后端再进行解析。
边栏推荐
- C语言基础[通俗易懂]
- 构建Buildroot根文件系统(I.MX6ULL)
- 【AGC】构建服务1-云函数示例
- awk statistical difference record
- The establishment of simple data cache layer
- uwp ScrollViewer content out of panel when set the long width
- Ant Group's time series database CeresDB is officially open source
- LED的C语言应用程序
- vehemently condemn
- QT(42)-QT线程-线程调用槽函数
猜你喜欢

Zero-knowledge proof notes - private transaction, pederson, interval proof, proof of ownership

QT(41)-多线程-QTThread-同步QSemaphore-互斥QMutex

腾讯云胡启明:Kubernetes云上资源的分析与优化

How to promote the implementation of rural revitalization

LED的C语言应用程序

Uniapp微信雪糕刺客单页小程序源码
![[Academic related] Tsinghua professor persuaded to quit his Ph.D.:I have seen too many doctoral students have mental breakdowns, mental imbalances, physical collapses, and nothing!...](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Academic related] Tsinghua professor persuaded to quit his Ph.D.:I have seen too many doctoral students have mental breakdowns, mental imbalances, physical collapses, and nothing!...

【AGC】构建服务1-云函数示例

Chrome安装zotero connector 插件

零知识证明笔记——私密交易,pederson,区间证明,所有权证明
随机推荐
Apache服务器配置多个站点
c sqlite...
Qt Designer生成的图形可以自适应窗口的大小变化
[TypeScript] In-depth study of TypeScript enumeration
Nuxt.js的优缺点和注意事项
Go study notes (Part 1) Configuring the Go development environment
"WAIC 2022 · hackers marathon" two ants wealth competition invited you to fight!
ts集成和使用
awk statistical difference record
简单理解 JS 事件循环
基于HDF的LED驱动程序开发(2)
动态数组底层是如何实现的
Chrome安装zotero connector 插件
QT(41)-多线程-QTThread-同步QSemaphore-互斥QMutex
【Web漏洞探索】跨站脚本漏洞
推荐系统_刘老师
C语言小笔记+题
uwp ScrollViewer content out of panel when set the long width
vim clear last search highlighting
EasyUi常用代码