Skip to content

Commit cb5c3f2

Browse files
author
YunaiV
committed
增加 spring session 的功能
1 parent 344af6b commit cb5c3f2

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.10.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>lab-26-distributed-session-01</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对 Spring MVC 的自动化配置 -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
22+
</dependencies>
23+
24+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab26.distributedsession;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.iocoder.springboot.lab26.distributedsession.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestParam;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import javax.servlet.http.HttpSession;
9+
import java.util.Enumeration;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
@RestController
14+
@RequestMapping("/session")
15+
public class SessionController {
16+
17+
@GetMapping("/set") // 其实 PostMapping 更合适,单纯为了方便
18+
public void set(HttpSession session,
19+
@RequestParam("key") String key,
20+
@RequestParam("value") String value) {
21+
session.setAttribute(key, value);
22+
}
23+
24+
@GetMapping("/get_all")
25+
public Map<String, Object> getAll(HttpSession session) {
26+
Map<String, Object> result = new HashMap<>();
27+
// 遍历
28+
for (Enumeration<String> enumeration = session.getAttributeNames();
29+
enumeration.hasMoreElements();) {
30+
String key = enumeration.nextElement();
31+
Object value = session.getAttribute(key);
32+
result.put(key, value);
33+
}
34+
// 返回
35+
return result;
36+
}
37+
38+
}

lab-26/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labs-parent</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-26</artifactId>
13+
<packaging>pom</packaging>
14+
<modules>
15+
<module>lab-26-distributed-session-01</module>
16+
</modules>
17+
18+
19+
</project>

0 commit comments

Comments
 (0)