javascript
SpringMvc @RequestParam、 @RequestBody、@RequestPart 的区别
注解@RequestParam接收的參數(shù)是來自requestHeader中,即請求頭。
RequestParam可以接受簡單類型的屬性,也可以接受對象類型。
@RequestParam有三個(gè)配置參數(shù):
- required 表示是否必須,默認(rèn)為 true,必須。
- defaultValue 可設(shè)置請求參數(shù)的默認(rèn)值。
- value 為接收url的參數(shù)名(相當(dāng)于key值)。
@RequestParam用來處理 Content-Type 為 application/x-www-form-urlencoded 編碼的內(nèi)容,Content-Type默認(rèn)為該屬性。@RequestParam也可用于其它類型的請求,例如:POST、DELETE等請求。
所以在postman中,要選擇body的類型為 x-www-form-urlencoded,這樣在headers中就自動(dòng)變?yōu)榱?Content-Type : application/x-www-form-urlencoded 編碼格式。
注解@RequestBody接收的參數(shù)是來自requestBody中,即請求體。
一般用于處理非 Content-Type: application/x-www-form-urlencoded編碼格式的數(shù)據(jù),比如:application/json、application/xml等類型的數(shù)據(jù)。
就application/json類型的數(shù)據(jù)而言,使用注解@RequestBody可以將body里面所有的json數(shù)據(jù)傳到后端,后端再進(jìn)行解析。
@RequestPart這個(gè)注解用在multipart/form-data表單提交請求的方法上。支持的請求方法的方式MultipartFile,屬于Spring的MultipartResolver類。這個(gè)請求是通過http協(xié)議傳輸?shù)摹?/strong>
@RequestParam也同樣支持multipart/form-data請求。
@RequestParam適用于[name-String]類型的請求域,@RequestPart適用于復(fù)雜的請求域(像JSON,XML)
總結(jié)
以上是生活随笔為你收集整理的SpringMvc @RequestParam、 @RequestBody、@RequestPart 的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么申请域名 制作网站(怎么申请域名 制
- 下一篇: 怎么申请域名抢注接口(怎样抢注域名)