@RequestParam,@PathVariable與其他一些與傳參相關(guān)的注解
我把@RequestParam,@PathVariable 搞混了,原因是我直接復(fù)制粘貼了(悲

@RequestParam?用于獲取URL中的查詢參數(shù)(即URL中以?
開頭的鍵值對)
例:
@GetMapping("/resfood/findByPage")?
public String findByPage(@RequestParam int pageno) {? ??
}
http://localhost:9001/resfood/findByPage?pageno=1

@PathVariable?用于獲取URL路徑中的參數(shù)值
例:
@GetMapping("/resfood/findByPage/{pageno}")?
public String findByPage(@PathVariable int pageno) {? ?
}
http://localhost:9001/resfood/findByPage/1?

@RequestBody
:作為控制器方法參數(shù)的注解,用于將請求正文(如JSON或XML)綁定到一個對象上。使用@RequestBody
注解時,Spring MVC會自動將請求的正文內(nèi)容反序列化為方法參數(shù)所對應(yīng)的對象,常見的格式可以是JSON、XML等(這個注解可以傳json格式的數(shù)據(jù))。該注解通常用于處理POST請求
@PostMapping("/example")?
public String example(@RequestBody User user) { ?}

@ModelAttribute:通過將請求參數(shù)的值綁定到方法參數(shù)或方法內(nèi)部對象中來獲取參數(shù)值??梢杂糜诨绢愋?、復(fù)雜對象或模型屬性。并不直接處理請求體,而是將請求參數(shù)綁定到方法參數(shù)或方法內(nèi)部對象的屬性上。適用于表單提交或查詢字符串。一般用于GET請求或表單提交。
@PostMapping("/example")?
public String example(@ModelAttribute User user) {?} // 不能傳json之類的數(shù)據(jù),可以傳對象

@RequestPart
:用于處理文件上傳,可以將上傳的文件綁定到MultipartFile對象上。
@PostMapping("/example")?
public String example(@RequestPart MultipartFile file) {?}