關(guān)于@RequestBody和@RequestParam注解的使用詳解
@RequestParam
@RequestParam:接收來自RequestHeader中,即請求頭。通常用于GET請求,例如: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請求
@GetMapping("/hello")
public String hello(@RequestParam(name = "id") Long id){
System.out.println("hello " + id);
return "hello message";
}
@RequestParam用來處理Content-Type 為 application/x-www-form-undencoded編碼的內(nèi)容,Content-Type 默認為該屬性
@RequestParam也可用于其它類型的請求,例如:POST、DELETE等請求。
POST請求
由于@RequestParam是用來處理 Content-Type 為 application/x-www-form-urlencoded 編碼的內(nèi)容的,所以在postman中,要選擇body的類型為 x-www-form-urlencoded,這樣在headers中就自動變?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";
}
如果前臺傳遞過來的參數(shù)不是三個,而是十個,如果繼續(xù)使用 @RequestParam 的方式來接收請求參數(shù),就需要十個 @RequestParam ,我們的代碼可讀性將會變得很差,并且當(dāng)參數(shù)類型相同時,十分容易出錯。所以使用實體類來接收傳遞過來的參數(shù),但是 @RequestParam 不支持直接傳遞實體類的方式
@Data
public class User {
@NotNull(message = "id不能為空")
private Long id;
@NotBlank(message = "名字不能為空")
private String name;
@NotBlank(message = "密碼不能為空")
private String password;
}// @RequestParam 不支持直接傳遞實體類的方式,所以可以在實體類中做數(shù)據(jù)校驗,使用 @Validated 注解使得作用在實體類屬性上的注解生效
@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 字符串來傳值的話,類型設(shè)置為 application/json,點擊發(fā)送的話,會報錯,后臺接收不到值,為 null

// console result: User(id=null, name=null, password=null)
@RequestBody
注解@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ù)傳到后端,后端再進行解析。
@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ù)是來自requestHeader中,即請求頭。通常用于GET請求,像POST、DELETE等其它類型的請求也可以使用。
注解@RequestBody接收的參數(shù)是來自requestBody中,即請求體。一般用于處理非 Content-Type: application/x-www-form-urlencoded編碼格式的數(shù)據(jù),比如:application/json、application/xml等類型的數(shù)據(jù)。通常用于接收POST、DELETE等類型的請求數(shù)據(jù),GET類型也可以適用。
在GET請求中,不能使用@RequestBody。在POST請求,可以使用@RequestBody和@RequestParam,但是如果使用@RequestBody,對于參數(shù)轉(zhuǎn)化的配置必須統(tǒng)一??梢允褂枚鄠€@RequestParam獲取數(shù)據(jù),@RequestBody不可以
舉個例子,在SpringMVC配置了HttpMessageConverters處理棧中,指定json轉(zhuǎn)化的格式,如Date轉(zhuǎn)成‘yyyy-MM-dd’,則參數(shù)接收對象包含的字段如果是Date類型,就只能讓客戶端傳遞年月日的格式,不能傳時分秒。因為不同的接口,它的參數(shù)可能對時間參數(shù)有不同的格式要求,所以這樣做會讓客戶端調(diào)用同事對參數(shù)的格式有點困惑,所以說擴展性不高。
如果使用@RequestParam來接受參數(shù),可以在接受參數(shù)的model中設(shè)置@DateFormat指定所需要接受時間參數(shù)的格式。
另外,使用@RequestBody接受的參數(shù)是不會被Servlet轉(zhuǎn)化統(tǒng)一放在request對象的Param參數(shù)集中,@RequestParam是可以的。
綜上所述,一般情況下,推薦使用@RequestParam注解來接受Http請求參數(shù)。
到此這篇關(guān)于關(guān)于@RequestBody和@RequestParam注解的使用詳解的文章就介紹到這了,更多相關(guān)@RequestBody和@RequestParam注解使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springcloud RestTemplate服務(wù)調(diào)用代碼實例
這篇文章主要介紹了Springcloud RestTemplate服務(wù)調(diào)用代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
SpringBoot基于Disruptor實現(xiàn)高效的消息隊列?
Disruptor是一個開源的Java框架,它被設(shè)計用于在生產(chǎn)者-消費者問題上獲得盡量高的吞吐量和盡量低的延遲,本文主要介紹了SpringBoot基于Disruptor實現(xiàn)高效的消息隊列?,具有一定的參考價值,感興趣的可以了解一下2024-02-02
如何使用Spring Validation優(yōu)雅地校驗參數(shù)
這篇文章主要介紹了如何使用Spring Validation優(yōu)雅地校驗參數(shù),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Java開發(fā)中常用的 Websocket 技術(shù)參考
WebSocket 使得客戶端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡單,允許服務(wù)端主動向客戶端推送數(shù)據(jù),當(dāng)然也支持客戶端發(fā)送數(shù)據(jù)到服務(wù)端。2020-09-09
mybatis取別名typeAliases標(biāo)簽的位置放錯導(dǎo)致報錯的解決
這篇文章主要介紹了mybatis取別名typeAliases標(biāo)簽的位置放錯導(dǎo)致報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09

