Spring中有哪几种方法获取HttpSession对象

Spring MVC

可以直接作为Controller的参数传入:

  1. @RequestMapping(value = "/test", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
  2. @ResponseBody
  3. public Map test(HttpSession session, String otherParam) {
  4. //TODO
  5. return null;
  6. }

其它

要获取HttpSession,只需要获取到HttpServletRequest,然后调用HttpServletRequest的getSession方法

1、注解自动注入

  1. @Autowired
  2. private HttpSession session;
  3. @Autowired
  4. private HttpServletRequest request;

2、普通类中获取

  1. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  2. request.getSession();
相关推荐
城管不管8 小时前
重生——第十一次面试之挖财一面2026.8.19已OC
java·服务器·jvm·数据库·spring·面试·职场和发展
今天AI了吗10 小时前
Python 基础语法(一):常量、变量、输入输出与运算符
开发语言·数据库·人工智能·python·sql·深度学习·机器学习
Csvn11 小时前
📊 SQL 入门 Day 22:视图与物化视图
后端·sql
亚历克斯神12 小时前
智能搜索系统的升级复盘——从 Elasticsearch 到混合检索的检索质量提升
java·spring·微服务
m0_5873830016 小时前
社区家政系统开发实战:从需求分析到上线部署全指南
java·spring boot·spring·需求分析
【赫兹威客】浩哥18 小时前
【无标题】
python·spring·django·pyqt
瀚高PG实验室19 小时前
SQL优化案例:存储过程、复杂SQL拆成简单SQL提升整体性能
数据库·sql·postgresql·瀚高数据库
Code额20 小时前
基于 SQLAlchemy 2.x,面向 MySQL 数据库的从入门到精通实战教程(2)
数据库·sql·mysql·adb·ai
cfm_291420 小时前
Spring AI Tool 调用架构全解
java·人工智能·spring
sun༒20 小时前
Spring @Scheduled 定时任务详解:Cron表达式、执行顺序、优先级与并行调度
java·后端·spring