@requestparam @param @pathvariable @requestbody的区别
生活随笔
收集整理的這篇文章主要介紹了
@requestparam @param @pathvariable @requestbody的区别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用來獲取前臺傳遞過來的參數,例如獲取以下鏈接的參數:
http://api.nc.com/api/item/category/list?pid=0
public String Demo1(@RequestParam String pid){
System.out.println(“鏈接中請求參數的id:”+pid);
return null;
}
路徑變量,即獲取鏈接路徑上的變量,使用restful風格(groups/{cid})時,經常使用,用來進行參數綁定。
鏈接:http://api.nc.com/api/item/groups/1
實例代碼:
@GetMapping(“groups/{cid}”)
public ResponseEntity<List> queryGroupsByCid(@PathVariable Long cid){
List list=this.specificationService.queryGroupsByCid(cid);
return ResponseEntity.ok(list);
}
上面代碼把restful中的變量cid的值,綁定到方法的參數上。且若參數名和需要綁定的restful中變量名稱不一致,需要在@PathVariable(“cid”) Long cid上綁定restful中的名稱。
一般用來處理content-type:"application/json,application/xml"兩種請求數據,并且能夠自動將傳遞過來的變量綁定到指定類,不用一個一個接收。
前臺傳遞的參數為:
實例代碼:
@PostMapping(“group”)
public ResponseEntity saveSpecGroup(@RequestBody SpecGroup specGroup){
this.specificationService.saveSpecGroup(specGroup);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
常用在sql語句中,實例代碼:
@Delete(“DELETE FROM tb_category_brand WHERE brand_id=#{bid}”)
void deleteByBrandId(@Param(“bid”) Long bid);
總結
以上是生活随笔為你收集整理的@requestparam @param @pathvariable @requestbody的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不入oracle数据库,Oracle数据
- 下一篇: oracle数据库常用的语法与复合函数