測試:
@PostMapping("mytest1")
public ResponseEntity fun1(int id){
System.out.println(id);
return ResponseEntity.ok().build();
}
以下測驗,代碼不變:
1:取自url,相關注解:@RequestParam
效果:成功
2:取自請求體:
-
a)、Content-Type爲x-www-form-urlencoded
效果:成功
-
b)、Content-Type爲form-data
效果:成功(因爲postman中選中form-data時,value支持爲text類型)
-
c)、Content-Type爲application/json
在postman中設置body爲raw。
效果:失敗
修改代碼:
以上可以總結出,SpringMVC中controller的方法參數默認取自url傳參,和兩種form表單傳參。如果需要使用json傳參,即請求參數是序列化的字符串時,則需手動指定某個參數:加上@RequestBody
註解。