国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

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

瀏覽:122日期: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
相關文章:
主站蜘蛛池模板: 桦川县| 嵊泗县| 铅山县| 肥乡县| 平山县| 六枝特区| 黄石市| 林甸县| 泾川县| 宁强县| 彩票| 定安县| 邛崃市| 虎林市| 彭泽县| 包头市| 逊克县| 姜堰市| 于都县| 宕昌县| 诸城市| 定远县| 吉林省| 三江| 三门县| 南安市| 宁强县| 香港 | 新龙县| 沧州市| 互助| 元氏县| 昌宁县| 遂宁市| 柳州市| 星子县| 屯门区| 定边县| 彝良县| 吉林市| 娄烦县|