在springMVC的controller中获取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);
}
使用springMVC的时候,有些时候会需要获取请求或者响应对象,例如在身份验证的时候,需要获取请求头中的token,在做登录系统的时候需要使用response对象向客户端添加cookie,一个有效的做法是在controller的方法中添加对应参数如下所示:
@RestController
public class Test2Contrller {
@RequestMapping("/test")
public void test(HttpServletRequest req, HttpServletResponse res) {
// todo
}
}
这样做有一个问题,就是如果这个系统是作为接口并希望被远程调用的,那么额外的参数的存在便会破坏原本的接口定义,造成麻烦,下面介绍两种不需要在方法中增加额外参数就能获取request和response的方式
第一种方式:通过RequestContextHolder类的方法获取requestAttributes,再从中获取请求和响应对象;
@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);
} }
}
第二种方式:可以将请求和响应对象抽取出来放在一个超类中,需要使用这两个对象的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);
} }
}
可以看到第二种方式代码简洁很多,如果对请求或者响应对象有大量的使用需求,例如需要从传过来的请求头中的token获取用户信息,动态的返回结果时,建议使用第二种方式;
在springMVC的controller中获取request,response对象的一个方法的更多相关文章
- spring mvc controller中获取request head内容
spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...
- SpringMVC Controller中注入Request成员域和在方法中定义中HttpServletRequest有啥区别
先说结论,在Controller中注入Request是线程安全的. 以下是解释: 我们先来看看这两者有什么不同 在controller注入成员变量request 可以看到注入的是一个代理对象 写在方法 ...
- 在Struts2的Action中获得request response session几种方法
转载自~ 在Struts2中,从Action中取得request,session的对象进行应用是开发中的必需步骤,那么如何从Action中取得这些对象呢?Struts2为我们提供了四种方式.分别为se ...
- Action类中获取request等对象的方法
struts2中的action类中,SevletActionContext可以获取
- js中获取当前url参数值的一个方法
var $_GET = (function(){ var url = window.document.location.href.toString();//获得当前url地址并 ...
- springMVC中获取request和response对象的几种方式(RequestContextHolder)
springMVC中获取request和response对象的几种方式 1.最简单方式:参数 2.加入监听器,然后在代码里面获取 原文链接:https://blog.csdn.net/weixin_4 ...
- SpringMvc中获取Request
Controller中加参数 @Controller public class TestController { @RequestMapping("/test") public v ...
- java中获取request与response对象的方法
Java 获取Request,Response对象方法 第一种.参数 @RequestMapping("/test") @ResponseBody public void sa ...
- Spring中获取request的几种方法,及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
随机推荐
- D3.js的v5版本入门教程(第八章)—— 坐标轴
D3.js的v5版本入门教程(第八章) D3中没有现成的坐标轴图形,需要我们自己用其他组件拼凑而成.D3中提供了坐标轴组件,使得我们在SVG中绘制一个坐标轴变得像添加一个普通元素那样简单 为了表绘制一 ...
- MacBook Air装Windows7双系统后的一些(未尝试)想法
转载请标注原地址:https://www.cnblogs.com/lixiaojing/p/11458477.html 运行环境: macOS在Mojave下的Boot Camp Assistant只 ...
- 第10组 Beta冲刺(2/5)
链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 新增修改用户信息.任务完成反馈等功能API 服务器后端部署,API接口的bet ...
- MySQL免安装配置步骤
此方式为直接解压压缩包安装MySQL 1.下载相关压缩包 官网下载地址:https://dev.mysql.com/downloads/mysql/ 下载后将压缩包解压至自己想要的文件路径即可 2.新 ...
- Java URLDecoder和URLEncoder对中文进行编码和解码
URLDecoder类包含一个decode(String s,String enc)静态方法,它可以将application/x-www-form-urlencoded MIME字符串转成普通字符串: ...
- 支付宝即时到账交易接口C#接入方式的几个坑
1.在官方文档中 https://docs.open.alipay.com/62/104743 可以清楚看到input_charset前面没有要求加下横杠,可是请求示例是带着的.经过实验得知,这个必须 ...
- Java基础 while 简单示例
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- nodejs 开发时,学用的热更新工具 nodemon
开发用最多的是重启再刷新页面,那热更新少不了, 工具有很多常用唯 nodemon 了, 安装: npm install -g nodemon // 建议全局安装,开发时用的工具 使用: nodemon ...
- delphi连接oracle数据库
使用ADO套接字连接: conn:= '(DESCRIPTION = '+ '(ADDRESS_LIST = '+ '(ADDRESS = (PROTOCOL = TCP)(HOST = '+host ...
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...