标签:代码示例 res ack http ssi 示例 pom spring servlet
创建项目
<!-- 配置spring session的依赖 --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-core</artifactId> <version>2.1.2.RELEASE</version> </dependency> <!-- 配置spring session data redis的依赖 --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>2.1.1.RELEASE</version> </dependency> <!-- springboot集成redis的起步依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
# 配置内嵌tomcat服务器信息
server.port=8080
server.servlet.context-path=/A07-springboot-session
#配置redis连接信息
spring.redis.host=192.168.132.128
spring.redis.port=6379
spring.redis.password=123456
spring.session.store-type=redis
代码示例
package com.bjpowernode.springboot.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpSession; @RestController public class SessionController { @GetMapping("/boot/set") public String setSession(HttpSession session){ session.setAttribute("url","http://www.bjpowernode.com"); return "set session ok"; } @GetMapping("/boot/get") public String getSession(HttpSession session){ String url= (String) session.getAttribute("url"); return url; } }
package com.bjpowernode.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @SpringBootApplication @EnableRedisHttpSession public class A07SpringbootSessionApplication { public static void main(String[] args) { SpringApplication.run(A07SpringbootSessionApplication.class, args); } }
注解@EnableRedisHttpSession启用redis作为session容器
设置地址:http://localhost:8080/A07-springboot-session/boot/set
获取地址:http://localhost:8080/A07-springboot-session/boot/get
标签:代码示例 res ack http ssi 示例 pom spring servlet
原文地址:https://www.cnblogs.com/Tpf386/p/11050379.html