關(guān)于@RequestBody和@RequestParam注解的使用詳解
@RequestParam
@RequestParam:接收來(lái)自RequestHeader中,即請(qǐng)求頭。通常用于GET請(qǐng)求,例如:http://localhost:8080/hello/name=admin&age=18
@Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RequestParam { @AliasFor("name") String value() default ""; @AliasFor("value") String name() default ""; boolean required() default true; String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n"; }
GET請(qǐng)求
@GetMapping("/hello") public String hello(@RequestParam(name = "id") Long id){ System.out.println("hello " + id); return "hello message"; }
@RequestParam用來(lái)處理Content-Type
為 application/x-www-form-undencoded
編碼的內(nèi)容,Content-Type
默認(rèn)為該屬性
@RequestParam也可用于其它類型的請(qǐng)求,例如:POST、DELETE等請(qǐng)求。
POST請(qǐng)求
由于@RequestParam是用來(lái)處理 Content-Type
為 application/x-www-form-urlencoded
編碼的內(nèi)容的,所以在postman中,要選擇body的類型為 x-www-form-urlencoded
,這樣在headers中就自動(dòng)變?yōu)榱?Content-Type
: application/x-www-form-urlencoded
編碼格式。如下圖所示:
@PostMapping("/save") public String hello(@RequestParam(name = "id") Long id, @RequestParam("name") String name, @RequestParam("password") String password){ System.out.println(user); return "hello message"; }
如果前臺(tái)傳遞過(guò)來(lái)的參數(shù)不是三個(gè),而是十個(gè),如果繼續(xù)使用 @RequestParam 的方式來(lái)接收請(qǐng)求參數(shù),就需要十個(gè) @RequestParam ,我們的代碼可讀性將會(huì)變得很差,并且當(dāng)參數(shù)類型相同時(shí),十分容易出錯(cuò)。所以使用實(shí)體類來(lái)接收傳遞過(guò)來(lái)的參數(shù),但是 @RequestParam 不支持直接傳遞實(shí)體類的方式
@Data public class User { @NotNull(message = "id不能為空") private Long id; @NotBlank(message = "名字不能為空") private String name; @NotBlank(message = "密碼不能為空") private String password; }
// @RequestParam 不支持直接傳遞實(shí)體類的方式,所以可以在實(shí)體類中做數(shù)據(jù)校驗(yàn),使用 @Validated 注解使得作用在實(shí)體類屬性上的注解生效 @PostMapping("/save") public String save(@Validated User user){ System.out.println(user); return "hello success"; } // console result: User(id=110, name=admin, password=123456)
如果改用 json
字符串來(lái)傳值的話,類型設(shè)置為 application/json
,點(diǎn)擊發(fā)送的話,會(huì)報(bào)錯(cuò),后臺(tái)接收不到值,為 null
// console result: User(id=null, name=null, password=null)
@RequestBody
注解@RequestBody接收的參數(shù)是來(lái)自requestBody中,即請(qǐng)求體。一般用于處理非 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)行解析。
@PostMapping("/saveBatch") public String saveBatch(@RequestBody @Validated List<User> list){ list.forEach(System.out::println); return "saveBatch success"; }
// console result: // User(id=1, name=admin, password=123456) // User(id=2, name=cheny, password=cheny)
傳遞到 Map 中
@PostMapping("/listMap") public String saveMap(@RequestBody List<Map<String, String>> listMap){ for (Map<String, String> map : listMap) { System.out.println(map); } return "listMap success"; }
// console result: // {id=1, name=admin} // {id=2, age=18}
總結(jié)
注解@RequestParam接收的參數(shù)是來(lái)自requestHeader中,即請(qǐng)求頭。通常用于GET請(qǐng)求,像POST、DELETE等其它類型的請(qǐng)求也可以使用。
注解@RequestBody接收的參數(shù)是來(lái)自requestBody中,即請(qǐng)求體。一般用于處理非 Content-Type: application/x-www-form-urlencoded
編碼格式的數(shù)據(jù),比如:application/json
、application/xml
等類型的數(shù)據(jù)。通常用于接收POST、DELETE等類型的請(qǐng)求數(shù)據(jù),GET類型也可以適用。
在GET請(qǐng)求中,不能使用@RequestBody。在POST請(qǐng)求,可以使用@RequestBody和@RequestParam,但是如果使用@RequestBody,對(duì)于參數(shù)轉(zhuǎn)化的配置必須統(tǒng)一??梢允褂枚鄠€(gè)@RequestParam獲取數(shù)據(jù),@RequestBody不可以
舉個(gè)例子,在SpringMVC配置了HttpMessageConverters處理?xiàng)V?,指定json轉(zhuǎn)化的格式,如Date轉(zhuǎn)成‘yyyy-MM-dd’,則參數(shù)接收對(duì)象包含的字段如果是Date類型,就只能讓客戶端傳遞年月日的格式,不能傳時(shí)分秒。因?yàn)椴煌慕涌?,它的參?shù)可能對(duì)時(shí)間參數(shù)有不同的格式要求,所以這樣做會(huì)讓客戶端調(diào)用同事對(duì)參數(shù)的格式有點(diǎn)困惑,所以說(shuō)擴(kuò)展性不高。
如果使用@RequestParam來(lái)接受參數(shù),可以在接受參數(shù)的model中設(shè)置@DateFormat指定所需要接受時(shí)間參數(shù)的格式。
另外,使用@RequestBody接受的參數(shù)是不會(huì)被Servlet轉(zhuǎn)化統(tǒng)一放在request對(duì)象的Param參數(shù)集中,@RequestParam是可以的。
綜上所述,一般情況下,推薦使用@RequestParam注解來(lái)接受Http請(qǐng)求參數(shù)。
到此這篇關(guān)于關(guān)于@RequestBody和@RequestParam注解的使用詳解的文章就介紹到這了,更多相關(guān)@RequestBody和@RequestParam注解使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決idea每次新建項(xiàng)目都需要重新指定maven目錄
這篇文章主要介紹了解決idea每次新建項(xiàng)目都需要配置maven,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09JPA中@CreatedDate和@LastModifiedDate的使用方式
這篇文章主要介紹了JPA中@CreatedDate和@LastModifiedDate的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11SpringController返回值和異常自動(dòng)包裝的問(wèn)題小結(jié)
今天遇到一個(gè)需求,在不改動(dòng)原系統(tǒng)代碼的情況下,將Controller的返回值和異常包裝到一個(gè)統(tǒng)一的返回對(duì)象中去,下面通過(guò)本文給大家介紹SpringController返回值和異常自動(dòng)包裝的問(wèn)題,需要的朋友可以參考下2024-03-03詳解spring boot使用@Retryable來(lái)進(jìn)行重處理
本篇文章主要介紹了詳解spring boot使用@Retryable來(lái)進(jìn)行重處理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Java?BasePooledObjectFactory?對(duì)象池化技術(shù)的使用
這篇文章主要介紹了Java?BasePooledObjectFactory?對(duì)象池化技術(shù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Java中實(shí)現(xiàn)Comparable和Comparator對(duì)象比較
這篇文章主要針對(duì)Java中Comparable和Comparator對(duì)象進(jìn)行比較,感興趣的小伙伴們可以參考一下2016-02-02