当前位置: 首页 > news >正文

SpringBoot中获取当前请求的request和response

在Spring Boot中,你可以以多种方式获取当前请求的HttpServletRequest和HttpServletResponse对象。以下是几种常见的写法示例:

1. 在方法参数中声明

最常见和推荐的方式是在控制器方法的参数中直接声明HttpServletRequest和HttpServletResponse对象。Spring Boot会自动将它们注入到方法中。

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@RestController
public class MyController {@GetMapping("/hello1")public String hello1(HttpServletRequest request, HttpServletResponse response) {// 使用 request 对象String method = request.getMethod();String uri = request.getRequestURI();// 使用 response 对象response.setContentType("text/plain");response.setStatus(HttpServletResponse.SC_OK);return "Hello 1, Spring Boot!";}
}

2. 使用 @RequestMappingHandlerAdapter

你可以通过注入RequestMappingHandlerAdapter来手动获取HttpServletRequest和HttpServletResponse对象。这种方式比较灵活,但相对较少使用。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@RestController
public class MyController {@Autowiredprivate RequestMappingHandlerAdapter handlerAdapter;@GetMapping("/hello2")public String hello2(HttpServletRequest request, HttpServletResponse response) throws Exception {// 通过 handlerAdapter 获取 request 和 response 对象HttpServletRequest req = (HttpServletRequest) handlerAdapter.getWebBindingInitializer().getBindingContext().getModel().get("org.springframework.web.servlet.HandlerMapping.uriTemplateVariables");// 使用 request 对象String method = req.getMethod();String uri = req.getRequestURI();// 使用 response 对象response.setContentType("text/plain");response.setStatus(HttpServletResponse.SC_OK);return "Hello 2, Spring Boot!";}
}

3. 使用 ThreadLocal

另一种方式是使用ThreadLocal来存储当前的HttpServletRequest和HttpServletResponse对象,然后在需要时从ThreadLocal中获取。

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@RestController
public class MyController {@GetMapping("/hello3")public String hello3() {// 从 RequestContextHolder 中获取 ServletRequestAttributesServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();// 获取 HttpServletRequest 对象HttpServletRequest request = attributes.getRequest();// 获取 HttpServletResponse 对象HttpServletResponse response = attributes.getResponse();// 使用 request 对象String method = request.getMethod();String uri = request.getRequestURI();// 使用 response 对象response.setContentType("text/plain");response.setStatus(HttpServletResponse.SC_OK);return "Hello 3, Spring Boot!";}
}

总结

以上是在Spring Boot中常见的几种方式获取当前请求的HttpServletRequest和HttpServletResponse对象。推荐使用第一种方式,即在方法参数中声明,因为它简单直观且符合Spring Boot的最佳实践。

http://www.lryc.cn/news/372849.html

相关文章:

  • Neo4j 桌面版打不开踩坑贴
  • [数据集][目标检测]中国象棋检测数据集VOC+YOLO格式300张12类别
  • 全方位·多层次·智能化,漫途水库大坝安全监测方案
  • windows安装SQLyog
  • jEasyUI 转换 HTML 表格为数据网格
  • 深度解析RocketMq源码-持久化组件(一) MappedFile
  • 贝壳APP渗透测试WP
  • IDEA快速入门02-快速入门
  • 快速构建本地RAG聊天机器人:使用LangFlow和Ollama实现无代码开发
  • 关于使用pycharm中控制台运行代码错误之FileNotFoundError: [Errno 2] No such file or directory:
  • 【SpringBoot】深入分析 SpringApplication 源码:彻底理解 SpringBoot 启动流程
  • 边界内聚和耦合
  • 单调栈——AcWing.830单调栈
  • 手机上安装AI模型是一种什么体验?
  • 【MySQL】主从复制
  • vscode插件开发之 - menu配置
  • 自学C语言-9
  • NVIDIA Triton系列01-应用概论
  • LIMS(实验室)信息管理系统源码、有哪些应用领域?采用C# ASP.NET dotnet 3.5 开发的一套实验室信息系统源码
  • Web前端进国企:挑战与机遇并存
  • 快速上手SpringBoot
  • SQL 快速参考
  • Cask ‘oraclexxx‘ is unavailable: No Cask with this name exists.
  • 2024年武汉市中级、高级职称水测考试开卷方法分享
  • 计算机网络(6) ICMP协议
  • FuTalk设计周刊-Vol.036
  • Java——面向对象进阶(三)
  • 鸿蒙开发电话服务:【@ohos.telephony.observer (observer)】
  • 希亦、追觅、云鲸洗地机:究竟有何不同?选择哪款更合适
  • 代码随想录算法训练营第二十六天