SpringMVC獲取HTTP中元素的實現(xiàn)示例
一、獲取URL中的參數(shù)@PathVariable
path variable: 路徑變量。
@PathVariable這個注解可以拿到URL中的參數(shù),使用注意:
- 寫在方法的參數(shù)名前,每個變量都要寫。
- 當(dāng)URL中的變量與方法中的變量同名時,可以不在@PathVariable寫上URL的變量名。
- 當(dāng)要對拿到的URL變量重命名,要在@PathVariable()括號中寫上URL的名字。
- URL中的參數(shù)必傳。
后端代碼:
Postman傳參:
二、上傳?件@RequestPart
- 上傳文件使用MuMultipartFile類下的transferTo方法。
- 使用@RequestPart注解可以進行重命名。
- 記得拋異常
后端代碼:
@RequestMapping("/r2") public String getFile(@RequestPart("file11") MultipartFile file) throws IOException { String fileName = file.getOriginalFilename(); file.transferTo(new File("E:/"+ file.getOriginalFilename())); return "上傳成功"+fileName; }
Postman傳參:
- 在Body里面找form-data傳File類型。
三、獲取Cookie/Session
3.1 HttpServletRequest和 HttpServletResponse
- HttpServletRequest這個類可以拿到HTTP請求中的東西。
- HttpServletResponse這個類可以拿到HTTP響應(yīng)中的東西,還可以進行修改。
3.2 獲取Cookie
獲取Cookie有以下兩種方式。
3.2.1 使用HttpServletRequest
只需要調(diào)用該類下的getCookies方法即可。
@RequestMapping("r3") public String r3 (HttpServletRequest request) { Cookie[] cookies = request.getCookies(); return "獲取成功"; }
Postman發(fā)請求:
3.2.2 使用注解@CookieValue
在注解中寫下Cookie中變量的名,然后后面跟著要賦值的變量類型與名字。
這種方法每次只能獲得一個Cookie變量。
@RequestMapping("/r4") public String r4(@CookieValue("name") String name) { return name; }
Postman發(fā)請求:
3.3 設(shè)置session
使用HttpServletRequest下的getSession方法,拿到sessionId,拿到session。
HttpSession getSession(boolean create);
- 默認(rèn)為true,如果沒有拿到session,返回一個空HttpSession對象。
- 如果為false,沒有拿到session,返回一個null。
代碼:
@RequestMapping("/r5") public String setSession(HttpServletRequest request) { HttpSession session = request.getSession(); session.setAttribute("name","lisi"); session.setAttribute("age","88"); return "設(shè)置成功"; }
3.4 獲取session
獲取session有以下三種方式。
3.4.1 使用HttpServletRequest
使用HttpServletRequest類下的getSession方法拿到session,
再通過HttpSession 類下的方法getAttribute獲取session中的對象 Object getAttribute(String name);
。
代碼:
@RequestMapping("/r6") public String r5(HttpServletRequest request) { HttpSession session = request.getSession(false); if (null == session) { return "用戶未登錄"; } else { return "用戶名:"+(String) session.getAttribute("name") + "用戶年齡"+(String) session.getAttribute("age"); } }
Postman傳參:
3.4.2 直接使用HttpSession
相當(dāng)于HttpSession session = request.getSession();
直接在方法參數(shù)完成。
@RequestMapping("/r8") public String r5(HttpSession session ) { if (null == session) { return "用戶未登錄"; } else { return "用戶名:"+(String) session.getAttribute("name") + "用戶年齡"+(String) session.getAttribute("age"); } }
3.4.3 使用注解@SessionValue
注解中value代表:session中的對象, required 調(diào)配沒拿到session時返回什么,true返回空HttpSession對象,false返回null。
代碼:
@RequestMapping("r7") public String r6(@SessionAttribute(value = "name",required = false) String name) { System.out.println(name); return name; }
Postman傳參:
四、獲取Header
獲取session有以下兩種方式。
4.1 使用HttpServletRequest
使用HttpServletRequest類下的getHeader方法,方法中填入想要的header中的參數(shù)名稱,拿到header下的參數(shù)。
代碼:
@RequestMapping("/r9") public String getHeader(HttpServletRequest request) { String header = request.getHeader("user-agent"); return "獲取到 "+header; }
Postman傳參:
4.2 使用注解@RequestHeader
在注解中寫下Header中參數(shù)的名,然后后面跟著要賦值給的變量類型與名字。
代碼:
@RequestMapping("/r10") public String getHeader2(@RequestHeader("user-agent") String header) { return "獲取到 "+header; }
Postman傳參:
到此這篇關(guān)于SpringMVC獲取HTTP中元素的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringMVC獲取HTTP元素內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?EasyExcel實現(xiàn)合并相同內(nèi)容單元格與動態(tài)標(biāo)題功能
這篇文章主要為大家詳細介紹了Java?EasyExcel如何實現(xiàn)合并相同內(nèi)容單元格與動態(tài)標(biāo)題功能,文中的示例代碼講解詳細,有需要的小伙伴可以參考下2023-12-12java中Webclient對象如何解析400狀態(tài)碼詳解
這篇文章主要介紹了java中Webclient對象如何解析400狀態(tài)碼的相關(guān)資料,文中通過代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用java具有一定的參考借鑒價值,需要的朋友可以參考下2024-12-12Windows系統(tǒng)中Java調(diào)用cmd命令及執(zhí)行exe程序的方法
這篇文章主要介紹了Windows系統(tǒng)中Java調(diào)用cmd命令及執(zhí)行exe程序的方法,主要用到了IOException類,需要的朋友可以參考下2016-03-03