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();
相关推荐
洋不写bug7 小时前
Spring IoC & DI 超详细解析
spring·di·javaee·spring ioc·类注解·方法注解·扫描路径
master3369 小时前
基于 Spring AI + Spring Cloud 构建 Web 版 AI Chat:多模型接入、流式对话与全链路验证
人工智能·spring·spring cloud
骑着蜗牛撵大象32713 小时前
AgentScope 搭大脑、Spring AI 当手脚:企业智能体推理与执行分层落地
java·人工智能·spring·智能体·工具调用·agentscope·模型编排
CoderYanger13 小时前
Java EE 进阶:15. Linux基本使用和程序部署(第二弹):部署 Web 项目到 Linux
linux·服务器·数据库·spring boot·spring·阿里云·java-ee
富贵儿99814 小时前
NL2SQL 实践手记 10:从命令行到网页查询——把整个系列串起来
数据库·python·sql·ai·自然语言处理·langchain
EatFan14 小时前
2026 后端 AI 集成选型拆解:Spring AI 2.0、MCP 协议与 Agent 内嵌三条路径的决策要点
java·人工智能·spring
这个DBA有点耶15 小时前
从逻辑日志到物理日志:实时数据同步的 DDL 与断点续传工程解
数据库·sql·程序人生·运维开发·dba
在角落发呆16 小时前
sql创建备份表全攻略:从 SELECT INTO 到跨机容灾
数据库·windows·sql·自动化·备份
长不大的杨瑞杰16 小时前
Spring AI 企业级 RAG 实战:检索、重排、结构化输出与提示注入防护
java·人工智能·spring·llm·rag·springai
weixin_3077791317 小时前
Python用正则把 SQL 关键字整词匹配后转成小写
开发语言·python·sql·算法