原文网址:SpringBoot--Controller获取HttpServletRequest_IT利刃出鞘的博客-CSDN博客

简介

本文介绍SpringBoot如何在Controller中获取HttpServletRequest。

法1:Controller的方法中加参数(线程安全)

简介

该方法实现的原理是,在Controller方法开始处理请求时,Spring会将request对象赋值到方法参数中。除了request对象,可以通过这种方法获取的参数还有很多。

Controller中获取request对象后,如果要在其他方法中(如service方法、工具类方法等)使用request对象,需要在调用这些方法时将request对象作为参数传入。

代码示例

这种方法实现最简单,直接上Controller代码:

​package com.example.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;@RestController
@RequestMapping
public class HelloController {@GetMapping("/test1")public String test1(HttpServletRequest request) {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(request);return request.toString();}
}

线程安全性

是线程安全的。

测试:接口中睡眠,模拟多个请求同时处理

直接用“代码示例”中的代码

Postman开两个窗口,都访问:http://localhost:8080/test1

结果:(两个不同的request,可见线程安全的)

org.apache.catalina.connector.RequestFacade@4613eaaa
org.apache.catalina.connector.RequestFacade@492b9e2e

注意:如果不加睡眠,结果可能是:两个相同的request,因为如果它能处理过来,就会用同一个request去接收了。

法2:自动注入(线程不安全)

代码示例

package com.example.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;@RestController
@RequestMapping
public class HelloController {@Autowiredprivate HttpServletRequest request;@GetMapping("/test1")public String test1() {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(request);return request.toString();}
}

法3:基类中自动注入(线程不安全)

说明

与方法2相比,将注入部分代码放入到了基类中。

代码示例

基类代码

public class BaseController {@Autowiredprotected HttpServletRequest request;
}

Controller代码

这里列举了BaseController的两个派生类,由于此时测试代码会有所不同,因此服务端测试代码没有省略;客户端也需要进行相应的修改(同时向2个url发送大量并发请求)。

@Controller
public class TestController extends BaseController {// 存储已有参数,用于判断参数value是否重复,从而判断线程是否安全public static Set<String> set = new ConcurrentSkipListSet<>();@RequestMapping("/test")public void test() throws InterruptedException {String value = request.getParameter("key");// 判断线程安全if (set.contains(value)) {System.out.println(value + "\t重复出现,request并发不安全!");} else {System.out.println(value);set.add(value);}// 模拟程序执行了一段时间Thread.sleep(1000);}
}
@Controller
public class Test2Controller extends BaseController {@RequestMapping("/test2")public void test2() throws InterruptedException {String value = request.getParameter("key");// 判断线程安全(与TestController使用一个set进行判断)if (TestController.set.contains(value)) {System.out.println(value + "\t重复出现,request并发不安全!");} else {System.out.println(value);TestController.set.add(value);}// 模拟程序执行了一段时间Thread.sleep(1000);}
}

法4:@ModelAttribute(线程不安全)

代码示例

下面这种方法及其变种(变种:将request和bindRequest放在子类中)在网上经常见到:

package com.example.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;@RestController
@RequestMapping
public class HelloController {protected HttpServletRequest request;@ModelAttributepublic void bindreq(HttpServletRequest request) {this.request = request;}@GetMapping("/test1")public String test1() {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(request);return request.toString();}
}

SpringBoot--Controller获取HttpServletRequest相关推荐

  1. Springboot Controller实体属性大写、第二个字母大写无法获取数据

    1.首字母大写 建议先看这篇文章 Springboot Controller参数映射之属性首字母小写第二字母大写无法映射问题分析_controller 参数首字母大写_万物皆字节的博客-CSDN博客 ...

  2. spring mvc DispatcherServlet详解之二---request通过Controller获取ModelAndView过程

    整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet通过request获取控制器Controller的过程,现在来讲解DispatcherServletDisp ...

  3. 转:Spring Boot 获取 HttpServletRequest 的方法

    转自: Spring Boot 获取 HttpServletRequest 的方法 - 简书本文介绍 Spring Boot 2 获取 HttpServletRequest 的方法. 目录 概述 方法 ...

  4. java 获取service_Java service层获取HttpServletRequest工具类的方法

    Java service层获取HttpServletRequest工具类的方法 大家都知道 能在Controller/action层获取HttpServletRequest,但是这里给大家备份的是从代 ...

  5. Java service层获取HttpServletRequest工具类的方法

    大家都知道 能在Controller/action层获取HttpServletRequest ,但是这里给大家备份的是从代码内部service层获取HttpServletRequest工具类. 具体如 ...

  6. SpringBoot之获取配置文件中的数据

    SpringBoot之获取配置文件中的数据 项目结构 配置application.properties book.author=Tom book.name=SpringBoot # spring.pr ...

  7. Controller获取ip

    Controller获取ip public class BaseController {protected ThreadLocal<HttpServletRequest> requestT ...

  8. SpringBoot静态获取 bean的三种方式,你学会了吗?

    欢迎关注方志朋的博客,回复"666"获面试宝典 来源:blog.csdn.net/showchi/article/details/97005720 注意:调用者要被spring管理 ...

  9. springboot实战 获取spring上下文的4种方式

    实际开发中我们经常需要通过spring上下文获取一些配置信息,本文阐述springboot应用获取spring上下文的几种方式. 方式一:实现ApplicationContextAware接口 imp ...

最新文章

  1. 数据结构课程设计---------最少换车次数问题
  2. BeetleX.Http.Clients V1.5发布
  3. php 开启命令模式,如何启用PhpStorm中的命令行工具
  4. python中head_Python(Head First)学习笔记:六
  5. BZOJ 2733: [HNOI2012]永无乡
  6. ECSHOP2.7.3删除后台左侧菜单中的云服务中心
  7. 如何在WES 7下使用EWF功能/HORM功能
  8. 水库河道应急广播系统解决方案
  9. 编译安装libmodbus库
  10. Fast R-CNN文章详细解读
  11. JQuery EasyUI 教程
  12. 从win10回退到win7的苦逼经历
  13. No converter for [class com.defei.sms.result.Result] with preset Content-Type ‘null‘
  14. 2014新浪校招笔试题:取水果(17年第一篇让人懵逼的面试题)
  15. GitChat·大数据 | 史上最详细的Hadoop环境搭建
  16. 11 贪吃蛇小游戏 js版本 + vue版本
  17. 粉丝问我,写CSDN博客到底为了什么?
  18. 小菜鸡的html初步教程(第十三章 使用WEB字体)
  19. 电脑屏幕扩展后电脑没有声音
  20. Kindle Touch 看pdf格式书籍

热门文章

  1. 前锋PHP的大并发集群技术,高并发下,php、swoole与redis实现的秒杀功能
  2. 先就业 再择业——2006届大学生的就业之路
  3. 如何下载特定版本的MySQL驱动
  4. fegin aop拦截
  5. Windows 7下阻止系统关机
  6. 手机怎么开启地震预警功能?
  7. 方建生:阿里巴巴助力打造数字经济县域发展新样本
  8. [娱评]著名相声演员侯耀文为何死不瞑目?
  9. 多人即时战斗游戏服务端系列[2]--90坦克Online游戏对象介绍以及渲染机制
  10. 3699元!首款5G户外旗舰手机AGM X5发布