您的位置:首頁技術文章
文章詳情頁

spring boot 常見http請求url參數獲取方法

瀏覽:36日期:2023-07-20 16:27:19

在定義一個Rest接口時通常會利用GET、POST、PUT、DELETE來實現數據的增刪改查;這幾種方式有的需要傳遞參數,后臺開發人員必須對接收到的參數進行參數驗證來確保程序的健壯性

GET:一般用于查詢數據,采用明文進行傳輸,一般用來獲取一些無關用戶信息的數據 POST:一般用于插入數據 PUT:一般用于數據更新 DELETE:一般用于數據刪除;一般都是進行邏輯刪除(即:僅僅改變記錄的狀態,而并非真正的刪除數據)

1、@PathVaribale 獲取url中的數據

請求URL:localhost:8080/hello/id 獲取id值

實現代碼如下:

@RestControllerpublicclass 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

輸出:

id:81name:hello

2、@RequestParam 獲取請求參數的值

獲取url參數值,默認方式,需要方法參數名稱和url參數保持一致

請求URL:localhost:8080/hello?id=1000

@RestControllerpublicclass 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

具體代碼如下:

@RestControllerpublicclass 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

@RestControllerpublicclass HelloController { @RequestMapping(value='/hello',method= RequestMethod.GET) public String sayHello(@RequestParam('userId') Integer id){ return'id:'+id; } }

輸出:

id:100

到此這篇關于spring boot 常見http請求url參數獲取方法的文章就介紹到這了,更多相關spring boot url參數獲取內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
国产综合久久一区二区三区