spring boot 常見http請求url參數(shù)獲取方法
在定義一個Rest接口時通常會利用GET、POST、PUT、DELETE來實現(xiàn)數(shù)據(jù)的增刪改查;這幾種方式有的需要傳遞參數(shù),后臺開發(fā)人員必須對接收到的參數(shù)進(jìn)行參數(shù)驗證來確保程序的健壯性
- GET:一般用于查詢數(shù)據(jù),采用明文進(jìn)行傳輸,一般用來獲取一些無關(guān)用戶信息的數(shù)據(jù)
- POST:一般用于插入數(shù)據(jù)
- PUT:一般用于數(shù)據(jù)更新
- DELETE:一般用于數(shù)據(jù)刪除;一般都是進(jìn)行邏輯刪除(即:僅僅改變記錄的狀態(tài),而并非真正的刪除數(shù)據(jù))
1、@PathVaribale 獲取url中的數(shù)據(jù)
請求URL:localhost:8080/hello/id 獲取id值
實現(xiàn)代碼如下:
@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
輸出:
id:81name:hello
2、@RequestParam 獲取請求參數(shù)的值
獲取url參數(shù)值,默認(rèn)方式,需要方法參數(shù)名稱和url參數(shù)保持一致
請求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中有多個參數(shù)時,如:
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參數(shù)值,執(zhí)行參數(shù)名稱方式
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
到此這篇關(guān)于spring boot 常見http請求url參數(shù)獲取方法的文章就介紹到這了,更多相關(guān)spring boot url參數(shù)獲取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java設(shè)計模式之單例模式Singleton Pattern詳解
這篇文章主要介紹了Java設(shè)計模式之單例模式Singleton Pattern詳解,一些常用的工具類、線程池、緩存,數(shù)據(jù)庫,數(shù)據(jù)庫連接池、賬戶登錄系統(tǒng)、配置文件等程序中可能只允許我們創(chuàng)建一個對象,這就需要單例模式,需要的朋友可以參考下2023-12-12
idea根據(jù)實體類生成數(shù)據(jù)庫表的流程步驟
在開發(fā)的時候,經(jīng)常會遇到數(shù)據(jù)庫表結(jié)構(gòu)設(shè)計無法滿足業(yè)務(wù)的需求,需要去改動數(shù)據(jù)庫表,我們就需要去設(shè)計數(shù)據(jù)庫的字段,然后又回來增加實體類里的字段,這樣很麻煩,所以本文給大家介紹了idea根據(jù)實體類生成數(shù)據(jù)庫表的流程步驟,需要的朋友可以參考下2024-12-12
IDEA 2020 無法啟動的解決辦法(啟動崩盤)附IDEA 2020 新功能
這篇文章主要介紹了IDEA 2020 無法啟動的解決辦法(啟動崩盤)附IDEA 2020 新功能,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Springboot Druid 自定義加密數(shù)據(jù)庫密碼的幾種方案
這篇文章主要介紹了Springboot Druid 自定義加密數(shù)據(jù)庫密碼的步驟,幫助大家更好的理解和使用springboot,感興趣的朋友可以了解下2020-12-12

