您好,登錄后才能下訂單哦!
這篇文章主要講解了“@RequestBody和@RequestParam注解如何使用”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“@RequestBody和@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"; }
@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
默認(rèn)為該屬性
@RequestParam也可用于其它類型的請求,例如:POST、DELETE等請求。
由于@RequestParam是用來處理 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)傳遞過來的參數(shù)不是三個(gè),而是十個(gè),如果繼續(xù)使用 @RequestParam 的方式來接收請求參數(shù),就需要十個(gè) @RequestParam ,我們的代碼可讀性將會(huì)變得很差,并且當(dāng)參數(shù)類型相同時(shí),十分容易出錯(cuò)。所以使用實(shí)體類來接收傳遞過來的參數(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
字符串來傳值的話,類型設(shè)置為 application/json
,點(diǎn)擊發(fā)送的話,會(huì)報(bào)錯(cuò),后臺(tái)接收不到值,為 null
// console result: User(id=null, name=null, password=null)
注解@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)行解析。
@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)
@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}
感謝各位的閱讀,以上就是“@RequestBody和@RequestParam注解如何使用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對@RequestBody和@RequestParam注解如何使用這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。