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