springMVC中怎么利用controller獲取request-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)springMVC中怎么利用controller獲取request,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

成都創(chuàng)新互聯(lián)公司長(zhǎng)期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為祁縣企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)、做網(wǎng)站,祁縣網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。

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í)候會(huì)需要獲取請(qǐng)求或者響應(yīng)對(duì)象,例如在身份驗(yàn)證的時(shí)候,需要獲取請(qǐng)求頭中的token,在做登錄系統(tǒng)的時(shí)候需要使用response對(duì)象向客戶端添加cookie,一個(gè)有效的做法是在controller的方法中添加對(duì)應(yīng)參數(shù)如下所示:

@RestControllerpublic class Test2Contrller {  @RequestMapping("/test")  public void test(HttpServletRequest req, HttpServletResponse res) {    // todo   }}

這樣做有一個(gè)問(wèn)題,就是如果這個(gè)系統(tǒng)是作為接口并希望被遠(yuǎn)程調(diào)用的,那么額外的參數(shù)的存在便會(huì)破壞原本的接口定義,造成麻煩,下面介紹兩種不需要在方法中增加額外參數(shù)就能獲取request和response的方式

第一種方式:通過(guò)RequestContextHolder類的方法獲取requestAttributes,再?gòu)闹蝎@取請(qǐng)求和響應(yīng)對(duì)象;

@RestControllerpublic class Test2Contrller {  @RequestMapping("/testreq")  public void test() {    // 獲得request對(duì)象,response對(duì)象    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);    }   }}

第二種方式:可以將請(qǐng)求和響應(yīng)對(duì)象抽取出來(lái)放在一個(gè)超類中,需要使用這兩個(gè)對(duì)象的controller繼承這個(gè)類,直接使用即可,代碼如下:

超類:

public class BaseController {  // 這些對(duì)象何以直接被子類使用  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();  }}

子類:

@RestControllerpublic 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);    }   }}

可以看到第二種方式代碼簡(jiǎn)潔很多,如果對(duì)請(qǐng)求或者響應(yīng)對(duì)象有大量的使用需求,例如需要從傳過(guò)來(lái)的請(qǐng)求頭中的token獲取用戶信息,動(dòng)態(tài)的返回結(jié)果時(shí),建議使用第二種方式;

看完上述內(nèi)容,你們對(duì)springMVC中怎么利用controller獲取request有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

文章名稱:springMVC中怎么利用controller獲取request-創(chuàng)新互聯(lián)
URL分享:http://bm7419.com/article18/cecsdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、建站公司、網(wǎng)頁(yè)設(shè)計(jì)公司、小程序開(kāi)發(fā)企業(yè)網(wǎng)站制作、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)