Spring?Boot中獲取request的三種方式及請求過程
本篇博客主要記錄request相關(guān)知識,也是開發(fā)當(dāng)中經(jīng)常遇到的,感興趣的跟小編一起學(xué)習(xí)吧!
一、請求過程
大概的流程:
1、通過請求,然后訪問到服務(wù)器,像java常用的服務(wù)器就是tomcat。
2、服務(wù)器收到請求將請求參數(shù)、請求內(nèi)容封裝成一個(gè)request和response對象,以參數(shù)的形式傳到我們的java程序。(這個(gè)在servlet最底層的接口當(dāng)中就會看到的,本篇文章不過多說了,后續(xù)找時(shí)間出一篇sevlet的博客)
3、這時(shí)候java程序就有了request和response對象,request當(dāng)中包含了請求參數(shù)、session、請求路徑等一些列關(guān)于調(diào)用接口的相關(guān)內(nèi)容。而response就是我們對于該請求做出的請求響應(yīng)。響應(yīng)內(nèi)容可以是各種參數(shù),或者頁面等等,最終封裝到response,響應(yīng)給服務(wù)器。
4、服務(wù)器將response響應(yīng)給客戶端。
二、獲取request的三種方式
2.1、可以封裝為靜態(tài)方法
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); HttpServletResponse response = servletRequestAttributes.getResponse();
2.2、controller的方法里面
@GetMapping(value = "") public String doSomething(HttpServletRequest request,HttpServletResponse response) { //... }
2.3、直接注入
@Autowired private HttpServletRequest request; @Autowired private HttpServletResponse response;
三、request常用API
3.1、request路徑相關(guān)
// 獲取請求方式: GET String getMethod() // 獲取虛擬目錄(上下文目錄):虛擬路徑通過在application配置當(dāng)中可以配置 // 配置:server.servlet.context-path=/aaa String getContextPath() // 獲取Servlet路徑:就是獲取的controller當(dāng)中配置的路徑 String getServletPath() /// 獲取請求參數(shù): id=3 // http://localhost:8080/aaa/test?id=3 String getQueryString() // 獲取請求URI:/servletDemo/demo1 // 上下文路徑+controller當(dāng)中配置的路徑 String getRequestURI() // 獲取完整請求路徑:http://localhost:8080/aaa/test String getRequestURL() // 獲取協(xié)議及版本:HTTP/1.1 String getProtocol() // 獲取客戶機(jī)的IP地址:192.168.0.0 String getRemoteAddr()
3.2、Header相關(guān)
// 獲取請求頭數(shù)據(jù),通過請求頭名稱獲取值 String getHeader(String name) // 獲取所有請求頭信息 Enumeration<String> getHeaderNames()
3.3、獲取請求體
// 獲取流對象-獲取字符輸入流,只能操作字符數(shù)據(jù) BufferedReader getReader() // 獲取流對象-獲取字節(jié)輸入流,既能操作字節(jié)也能操作字符 ServletInputStream getInputStream()
3.4、獲取參數(shù)
// 獲取請求參數(shù)通用方式(根據(jù)參數(shù)名稱獲取參數(shù)值) username=zhangsan String getParameter(String name) // 獲取請求參數(shù)名稱獲取參數(shù)值的數(shù)組 hobby=xx&hobby=game String[] getParameters(String name) // 獲取請求參數(shù)名稱(與獲取請求頭方法類似) Enumeration<String> getParameterNames() // 獲取所有參數(shù)的Map集合 Map<String,String> getParameterMap()
3.5、中文亂碼
中文亂碼問題,Tomcat8 已經(jīng)將GET 請求方式的亂碼問題 解決了,但是POST方式的中文亂碼問題 依舊存在,需要自行配置
在獲取參數(shù)之前配置
request.setCharacterEncoding("UTF-8");
3.6、轉(zhuǎn)發(fā)
瀏覽器像服務(wù)器發(fā)起一次請求,服務(wù)器內(nèi)部實(shí)現(xiàn)轉(zhuǎn)發(fā),相當(dāng)于執(zhí)行了兩個(gè)資源之間的操作。
// 通過request對象獲取請求轉(zhuǎn)發(fā)服務(wù)器: RequestDispatcher getRequestDispatcher(String path); // 使用==RequestDispatcher對象來進(jìn)行轉(zhuǎn)發(fā) forward(ServlertRequest request,ServletResponse response);
3.7、共享數(shù)據(jù)
域?qū)ο螅阂粋€(gè)有作用范圍的對象,可以在范圍內(nèi)共享數(shù)據(jù)
request域:代表一次請求的范圍,一般用于請求轉(zhuǎn)發(fā)的多個(gè)資源中共享數(shù)據(jù)
// 存儲數(shù)據(jù) setAttribute(String name,Object obj) // 通過鍵來獲取值 Object getAttribute(String name); // 通過鍵移除鍵對應(yīng)的值 removeAttribute(String name)
四、response常用API
//設(shè)置狀態(tài)碼 response.setStatus(304); // 設(shè)置指定名稱響應(yīng)頭的值,下面是導(dǎo)出的時(shí)候我們經(jīng)常要設(shè)置的響應(yīng)頭,響應(yīng)頭當(dāng)中還有文件的名稱,也就是通過流下載文件的時(shí)候那個(gè)文件名稱 response.setHeader("Content-Disposition", "attachment;filename=" + zipName); // 1.字符輸出流 PrintWriter getWriter() // 2.字節(jié)輸出流(一般導(dǎo)出就是將文件寫到字節(jié)流,然后響應(yīng)response) ServletOutputStream getOutputStream() // 注意:在同一個(gè)Servlet,二種輸出流不能同時(shí)使用,產(chǎn)生互斥 // response提供了專門負(fù)責(zé)重定向的方法 response.sendRedirect("/項(xiàng)目地址/資源地址"); // 指定服務(wù)器響應(yīng)中文的編碼方式 response.setCharacterEncoding("GBK"); // 統(tǒng)一服務(wù)器和客戶端的編碼方式 response.setContentType("text/html;charset=utf-8"); // 導(dǎo)出的時(shí)候如果需要直接通過response流導(dǎo)出Excel,需要和前端設(shè)置上下文類型,如下: response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
五、常用工具類
5.1、封裝的
import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class ServletUtils { /** * 獲取request */ public static HttpServletRequest getRequest() { return getRequestAttributes().getRequest(); } * 獲取response public static HttpServletResponse getResponse() { return getRequestAttributes().getResponse(); * 獲取session public static HttpSession getSession() { return getRequest().getSession(); * request和response在ServletRequestAttributes類當(dāng)中 * * @return public static ServletRequestAttributes getRequestAttributes() { RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); return (ServletRequestAttributes) attributes; * 將字符串渲染到客戶端(前后端分離很少會用到) * @param response 渲染對象 * @param string 待渲染的字符串 * @return null public static String renderString(HttpServletResponse response, String string) { try { response.setStatus(200); response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); response.getWriter().print(string); } catch (IOException e) { e.printStackTrace(); } return null; * 獲取完整的請求路徑,包括:域名,端口,上下文訪問路徑 * 上傳圖片的時(shí)候需要:服務(wù)器路徑+上下文訪問路徑(所以封裝了該方法) * @return 服務(wù)地址 public static String getUrl() { HttpServletRequest request = ServletUtils.getRequest(); return getDomain(request); public static String getDomain(HttpServletRequest request) { StringBuffer url = request.getRequestURL(); String contextPath = request.getServletContext().getContextPath(); return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString(); }
5.2、Hutool工具類
Hutool工具類是目前最常用的工具類,他的官網(wǎng)寫的非常好,并且API也非常好,并且全是中文的,這是重點(diǎn)哈哈。
ServletUtil在線API文檔:https://apidoc.gitee.com/dromara/hutool
API當(dāng)中包含了:
- cookie相關(guān)
- 獲取請求體
- header相關(guān)
- 獲得MultiPart表單內(nèi)容
- 獲得請求參數(shù)、將參數(shù)轉(zhuǎn)換為Bean
- 以及response相關(guān)write操作
到此這篇關(guān)于Spring Boot當(dāng)中獲取request的三種方式的文章就介紹到這了,更多相關(guān)Spring Boot獲取request方式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis(mybatis-plus)映射文件(XML文件)中特殊字符轉(zhuǎn)義的實(shí)現(xiàn)
XML 文件在解析時(shí)會將五種特殊字符進(jìn)行轉(zhuǎn)義,本文主要介紹了mybatis(mybatis-plus)映射文件(XML文件)中特殊字符轉(zhuǎn)義的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12如何在Springboot實(shí)現(xiàn)攔截器功能
其實(shí)spring boot攔截器的配置方式和springMVC差不多,只有一些小的改變需要注意下就ok了,下面這篇文章主要給大家介紹了關(guān)于如何在Springboot實(shí)現(xiàn)攔截器功能的相關(guān)資料,需要的朋友可以參考下2022-06-06MyBatis-Plus 如何實(shí)現(xiàn)連表查詢的示例代碼
這篇文章主要介紹了MyBatis-Plus 如何實(shí)現(xiàn)連表查詢的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(2)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07Java7和Java8中的ConcurrentHashMap原理解析
這篇文章主要介紹了Java7和Java8中的ConcurrentHashMap原理解析,對ConcurrentHashMap感興趣的讀者,一定要好好看一下2021-04-04Spring Boot2發(fā)布調(diào)用REST服務(wù)實(shí)現(xiàn)方法
這篇文章主要介紹了Spring Boot2發(fā)布調(diào)用REST服務(wù)實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04