如何在springMVC的controller中獲取request
這篇文章主要介紹了如何在springMVC的controller中獲取request,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
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的時候,有些時候會需要獲取請求或者響應(yīng)對象,例如在身份驗證的時候,需要獲取請求頭中的token,在做登錄系統(tǒng)的時候需要使用response對象向客戶端添加cookie,一個有效的做法是在controller的方法中添加對應(yīng)參數(shù)如下所示:
@RestController
public class Test2Contrller {
@RequestMapping("/test")
public void test(HttpServletRequest req, HttpServletResponse res) {
// todo
}
}
這樣做有一個問題,就是如果這個系統(tǒng)是作為接口并希望被遠程調(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)對象抽取出來放在一個超類中,需要使用這兩個對象的controller繼承這個類,直接使用即可,代碼如下:
超類:
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é)果時,建議使用第二種方式;
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis之解決collection一對多問題(顯示的結(jié)果沒有整合到一起)
這篇文章主要介紹了Mybatis之解決collection一對多問題(顯示的結(jié)果沒有整合到一起),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Java實現(xiàn)ATM系統(tǒng)超全面步驟解讀建議收藏
這篇文章主要為大家詳細介紹了用Java實現(xiàn)簡單ATM機功能,文中實現(xiàn)流程寫的非常清晰全面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Springboot2.x+ShardingSphere實現(xiàn)分庫分表的示例代碼
這篇文章主要介紹了Springboot2.x+ShardingSphere實現(xiàn)分庫分表的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
淺談JAVA字符串匹配算法indexOf函數(shù)的實現(xiàn)方法
這篇文章主要介紹了淺談字符串匹配算法indexOf函數(shù)的實現(xiàn)方法,indexOf函數(shù)我們可以查找一個字符串(模式串)是否在另一個字符串(主串)出現(xiàn)過。對此感興趣的可以來了解一下2020-07-07
Spring Aop之AspectJ注解配置實現(xiàn)日志管理的方法
下面小編就為大家分享一篇Spring Aop之AspectJ注解配置實現(xiàn)日志管理的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
詳解SpringCloud Gateway 2020.0.2最新版
這篇文章主要介紹了SpringCloud Gateway 2020.0.2最新版,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04

