在定義一個Rest接口時一般會利用GET、POST、PUT、DELETE來實現數據的增刪改查;這幾種方式有的須要傳遞參數,後臺開發人員必須對接收到的參數進行參數驗證來確保程序的健壯性java
GET:通常用於查詢數據,採用明文進行傳輸,通常用來獲取一些無關用戶信息的數據spring
POST:通常用於插入數據瀏覽器
PUT:通常用於數據更新app
DELETE:通常用於數據刪除;通常都是進行邏輯刪除(即:僅僅改變記錄的狀態,而並不是真正的刪除數據)spring-boot
請求URL:localhost:8080/hello/id 獲取id值
url
實現代碼以下:code
@RestController publicclass HelloController { @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET) public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){ return"id:"+id+" name:"+name; } }
在瀏覽器中 輸入地址:接口
localhost:8080/hello/100/hello
開發
輸出:class
id:81name:hello
獲取url參數值,默認方式,須要方法參數名稱和url參數保持一致
請求URL:localhost:8080/hello?id=1000
@RestController publicclass HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String sayHello(@RequestParam Integer id){ return"id:"+id; } }
輸出:id:100
url中有多個參數時,如:
localhost:8080/hello?id=98&&name=helloworld
具體代碼以下:
@RestController publicclass HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String sayHello(@RequestParam Integer id,@RequestParam String name){ return"id:"+id+ " name:"+name; } }
獲取url參數值,執行參數名稱方式
localhost:8080/hello?userId=1000
@RestController publicclass HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String sayHello(@RequestParam("userId") Integer id){ return"id:"+id; } }
輸出:id:100