1. @RequestParam
該標籤用於接受請求頭中的參數,該標籤配置了四個參數:
public @interface RequestParam {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
boolean required() default true;
String defaultValue() default ValueConstants.DEFAULT_NONE;
}
value
,name
,用來接收請求的參數,與請求參數的 Key 值相互綁定。(注:@AliasFor 表示別名,它可以註解到自定義註解的兩個屬性上,表示這兩個互爲別名,也就是說這兩個屬性其實同一個含義)required
,表示該請求參數是否爲必須傳入,默認爲 true,即必須傳入。defaultValue
,設置請求參數傳入爲空,或沒有傳入該參數時,使用的默認值,當然對應 required 參數需要設置爲 false。
2. @RequestBody
該標籤用於接受請求中的請求體,該標籤只有一個參數:
public @interface RequestBody {
boolean required() default true;
}
required
,與 @RequestParam 標籤相同,表示該請求參數是否爲必須傳入,這裏指是否需要傳遞請求體,默認爲 true,即必須傳入。
3. 不同點
3.1 處理的請求的 Content-Type 不同
- @RequestParam 可處理 Content-Type 爲 application/x-www-form-urlencoded 編碼的內容的請求,Content-Type默認爲該屬性。@RequestParam 也可用於其它類型的請求,例如:POST、DELETE等請求。
- @RequestBody 接收的參數是來自 requestBody 中,即請求體。一般用於處理非 Content-Type: application/x-www-form-urlencoded 編碼格式的數據,比如:application/json 等類型的數據。
- @RequestParam 和 @RequestBody 分別適用於表單提交以及 json 提交方式過來的數據
3.2 GET請求
- @RequestParam 可以相應 GET 請求。
- 由於 GET 請求沒有請求體,@RequestBody 不用於該場景。
4. 用法示例
4.1 @RequestParam
4.1.1 接收 URL 上的參數
例如獲取下圖 URL 中,後面帶的參數
代碼:通過兩個 @RequestParam 指定屬性的 Key 獲取相應的值
@GetMapping(value = "test1")
@ResponseBody
public String test1(@RequestParam(value = "token") String token,
@RequestParam(value = "accountId") String accountId) {
System.out.println("token: " + token + "\naccountId" + accountId);
return "hello world";
}
4.1.2 接收 application/x-www-form-urlencoded 請求參數
請求頭:
請求體:
代碼:
@PostMapping(value = "test2")
@ResponseBody
public String test2(@RequestParam(value = "length") String length,
@RequestParam(value = "md5") String md5) {
System.out.println("length: " + length + "\nmd5: " + md5);
return "hello world";
}
4.2 @RequestBody
4.2.1 實體類映射 JSON 對象
這裏先構建一個實體類用於接收請求:
public class Person {
private Integer age;
private String name;
private Double weight;
// 這裏省略 get set 方法以及構造函數
}
構建接收請求的方法,直接將實體類使用 @RequestBody 標籤,會自動將 JSON 數據封裝爲實體類:
@PostMapping(value = "test3")
@ResponseBody
public String test3(@RequestBody Person person) {
return person.toString();
}
發送請求時,只需要指定:Content-Type 爲 application/json,再直接拼接 JSON 即可:
4.2.1 Map 映射 JSON
但是,對於每個請求,我們有時候不一定都可以構建一個實體類與之對應,這時候可以採用 Map 的方式,進行接收,例如我們還是上面例子中傳入的 JSON 數組,這時的 JAVA 代碼可以改變爲:
@PostMapping(value = "test4")
@ResponseBody
public String test4(@RequestBody Map<String, String> map) {
Person person = new Person();
person.setAge(Integer.parseInt(map.get("age")));
person.setName(map.get("name"));
person.setWeight(Double.parseDouble(map.get("weight")));
return person.toString();
}
4.3 @RequestParam & @RequestBody
這兩個類型的標籤,是可以同時使用的,例如在 URL 中添加參數,同時在 RequestBody 中新增 JSON 數組:
接收請求的代碼:
@PostMapping(value = "test5")
@ResponseBody
public String test5(@RequestParam(value = "token") String token,
@RequestParam(value = "accountId") String accountId,
@RequestBody Map<String, String> map) {
Person person = new Person();
person.setAge(Integer.parseInt(map.get("age")));
person.setName(map.get("name"));
person.setWeight(Double.parseDouble(map.get("weight")));
return person.toString();
}
以上爲對兩個標籤的整理,相關 Demo 代碼已上傳 github