如何在springMVC的controller中獲取request
這篇文章主要介紹了如何在springMVC的controller中獲取request,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); HttpServletResponse response = attributes.getResponse(); try { response.getWriter().write("hello"); } catch (IOException e) { e.printStackTrace(); } Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String value = request.getHeader(name); System.out.println(name + "===========" + value); }
使用springMVC的時(shí)候,有些時(shí)候會需要獲取請求或者響應(yīng)對象,例如在身份驗(yàn)證的時(shí)候,需要獲取請求頭中的token,在做登錄系統(tǒng)的時(shí)候需要使用response對象向客戶端添加cookie,一個(gè)有效的做法是在controller的方法中添加對應(yīng)參數(shù)如下所示:
@RestController public class Test2Contrller { @RequestMapping("/test") public void test(HttpServletRequest req, HttpServletResponse res) { // todo } }
這樣做有一個(gè)問題,就是如果這個(gè)系統(tǒng)是作為接口并希望被遠(yuǎn)程調(diào)用的,那么額外的參數(shù)的存在便會破壞原本的接口定義,造成麻煩,下面介紹兩種不需要在方法中增加額外參數(shù)就能獲取request和response的方式
第一種方式:通過RequestContextHolder類的方法獲取requestAttributes,再從中獲取請求和響應(yīng)對象;
@RestController public class Test2Contrller { @RequestMapping("/testreq") public void test() { // 獲得request對象,response對象 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); HttpServletResponse response = attributes.getResponse(); try { response.getWriter().write("hello"); } catch (IOException e) { e.printStackTrace(); } Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String value = request.getHeader(name); System.out.println(name + "===========" + value); } } }
第二種方式:可以將請求和響應(yīng)對象抽取出來放在一個(gè)超類中,需要使用這兩個(gè)對象的controller繼承這個(gè)類,直接使用即可,代碼如下:
超類:
public class BaseController { // 這些對象何以直接被子類使用 protected HttpServletRequest request; protected HttpServletResponse response; protected HttpSession session; @ModelAttribute public void setReqAndRes(HttpServletRequest req, HttpServletResponse res) { this.request = req; this.response = res; this.session = req.getSession(); } }
子類:
@RestController public class Test3Contrller extends BaseController{ @RequestMapping("/testreq2") public void test() { try { response.getWriter().write("hello"); } catch (IOException e) { e.printStackTrace(); } Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String value = request.getHeader(name); System.out.println(name + "===========" + value); } } }
可以看到第二種方式代碼簡潔很多,如果對請求或者響應(yīng)對象有大量的使用需求,例如需要從傳過來的請求頭中的token獲取用戶信息,動態(tài)的返回結(jié)果時(shí),建議使用第二種方式;
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java迭代器基礎(chǔ)知識點(diǎn)總結(jié)
在本篇內(nèi)容里小編給大家整理了一篇關(guān)于java迭代器基礎(chǔ)知識點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-01-01Mybatis之解決collection一對多問題(顯示的結(jié)果沒有整合到一起)
這篇文章主要介紹了Mybatis之解決collection一對多問題(顯示的結(jié)果沒有整合到一起),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Java實(shí)現(xiàn)ATM系統(tǒng)超全面步驟解讀建議收藏
這篇文章主要為大家詳細(xì)介紹了用Java實(shí)現(xiàn)簡單ATM機(jī)功能,文中實(shí)現(xiàn)流程寫的非常清晰全面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫分表的示例代碼
這篇文章主要介紹了Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫分表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10淺談JAVA字符串匹配算法indexOf函數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了淺談字符串匹配算法indexOf函數(shù)的實(shí)現(xiàn)方法,indexOf函數(shù)我們可以查找一個(gè)字符串(模式串)是否在另一個(gè)字符串(主串)出現(xiàn)過。對此感興趣的可以來了解一下2020-07-07Spring Aop之AspectJ注解配置實(shí)現(xiàn)日志管理的方法
下面小編就為大家分享一篇Spring Aop之AspectJ注解配置實(shí)現(xiàn)日志管理的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01詳解SpringCloud Gateway 2020.0.2最新版
這篇文章主要介紹了SpringCloud Gateway 2020.0.2最新版,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04