關于@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
編碼的內容,Content-Type
默認為該屬性
@RequestParam也可用于其它類型的請求,例如:POST、DELETE等請求。
POST請求
由于@RequestParam是用來處理 Content-Type
為 application/x-www-form-urlencoded
編碼的內容的,所以在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 ,我們的代碼可讀性將會變得很差,并且當參數(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
字符串來傳值的話,類型設置為 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}
總結
注解@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ù)轉化的配置必須統(tǒng)一??梢允褂枚鄠€@RequestParam獲取數(shù)據(jù),@RequestBody不可以
舉個例子,在SpringMVC配置了HttpMessageConverters處理棧中,指定json轉化的格式,如Date轉成‘yyyy-MM-dd’,則參數(shù)接收對象包含的字段如果是Date類型,就只能讓客戶端傳遞年月日的格式,不能傳時分秒。因為不同的接口,它的參數(shù)可能對時間參數(shù)有不同的格式要求,所以這樣做會讓客戶端調用同事對參數(shù)的格式有點困惑,所以說擴展性不高。
如果使用@RequestParam來接受參數(shù),可以在接受參數(shù)的model中設置@DateFormat指定所需要接受時間參數(shù)的格式。
另外,使用@RequestBody接受的參數(shù)是不會被Servlet轉化統(tǒng)一放在request對象的Param參數(shù)集中,@RequestParam是可以的。
綜上所述,一般情況下,推薦使用@RequestParam注解來接受Http請求參數(shù)。
到此這篇關于關于@RequestBody和@RequestParam注解的使用詳解的文章就介紹到這了,更多相關@RequestBody和@RequestParam注解使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JPA中@CreatedDate和@LastModifiedDate的使用方式
這篇文章主要介紹了JPA中@CreatedDate和@LastModifiedDate的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11SpringController返回值和異常自動包裝的問題小結
今天遇到一個需求,在不改動原系統(tǒng)代碼的情況下,將Controller的返回值和異常包裝到一個統(tǒng)一的返回對象中去,下面通過本文給大家介紹SpringController返回值和異常自動包裝的問題,需要的朋友可以參考下2024-03-03詳解spring boot使用@Retryable來進行重處理
本篇文章主要介紹了詳解spring boot使用@Retryable來進行重處理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06Java?BasePooledObjectFactory?對象池化技術的使用
這篇文章主要介紹了Java?BasePooledObjectFactory?對象池化技術,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04Java中實現(xiàn)Comparable和Comparator對象比較
這篇文章主要針對Java中Comparable和Comparator對象進行比較,感興趣的小伙伴們可以參考一下2016-02-02