Skip to content

Commit d449b10

Browse files
committed
feat:Spring Boot 整合 WebFlux 实现基本的 CURD。
1 parent f51acd2 commit d449b10

File tree

10 files changed

+553
-0
lines changed

10 files changed

+553
-0
lines changed

springboot-webflux/.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/

springboot-webflux/README.md

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
# 一、工程简介
2+
> Spring2.X新系列的框架搭建 >>> WebFlux + RESTFul风格。
3+
4+
# 二、搭建步骤
5+
1. 引入核心依赖
6+
```xml
7+
<dependencies>
8+
<dependency>
9+
<groupId>org.springframework.boot</groupId>
10+
<artifactId>spring-boot-starter-web</artifactId>
11+
</dependency>
12+
<dependency>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-webflux</artifactId>
15+
</dependency>
16+
<dependency>
17+
<groupId>org.projectlombok</groupId>
18+
<artifactId>lombok</artifactId>
19+
<optional>true</optional>
20+
</dependency>
21+
</dependencies>
22+
```
23+
2. 定义测试用的实体类`User.java`
24+
```java
25+
@Data
26+
@Builder
27+
@Accessors(chain = true)
28+
@NoArgsConstructor
29+
@AllArgsConstructor
30+
public class User {
31+
private Long id;
32+
private String username;
33+
private String realName;
34+
private String password;
35+
private Integer age;
36+
private String sex;
37+
}
38+
```
39+
3. 定义DAO层
40+
```java
41+
@Repository
42+
public class UserRepository {
43+
/**
44+
* 可以替换成 ORM框架(mybatis的*Mapper)
45+
*/
46+
private ConcurrentMap<Long, User> repository = new ConcurrentHashMap<>();
47+
48+
private static final AtomicLong idGenerator = new AtomicLong(0);
49+
50+
public Long save(User user) {
51+
Long id = idGenerator.incrementAndGet();
52+
user.setId(id);
53+
repository.put(id, user);
54+
return id;
55+
}
56+
57+
public Collection<User> findAll() {
58+
return repository.values();
59+
}
60+
61+
public User findUserById(Long id) {
62+
return repository.get(id);
63+
}
64+
65+
public Long updateUser(User user) {
66+
repository.put(user.getId(), user);
67+
return user.getId();
68+
}
69+
70+
public Long deleteUser(Long id) {
71+
repository.remove(id);
72+
return id;
73+
}
74+
}
75+
```
76+
4. 定义 Service层(即 *Handler)
77+
```java
78+
@Component
79+
public class UserHandler {
80+
81+
private final UserRepository userRepository;
82+
83+
@Autowired
84+
public UserHandler(UserRepository userRepository) {
85+
this.userRepository = userRepository;
86+
}
87+
88+
public Mono<Long> save(User user) {
89+
return Mono.create(userMonoSink -> userMonoSink.success(userRepository.save(user)));
90+
}
91+
92+
public Mono<User> findUserById(Long id) {
93+
return Mono.justOrEmpty(userRepository.findUserById(id));
94+
}
95+
96+
public Flux<User> findAllUser() {
97+
return Flux.fromIterable(userRepository.findAll());
98+
}
99+
100+
public Mono<Long> modifyUser(User user) {
101+
return Mono.create(userMonoSink -> userMonoSink.success(userRepository.updateUser(user)));
102+
}
103+
104+
public Mono<Long> deleteUser(Long id) {
105+
return Mono.create(userMonoSink -> userMonoSink.success(userRepository.deleteUser(id)));
106+
}
107+
}
108+
```
109+
5. 控制层(`UserWebFluxController.java`
110+
```java
111+
@RestController
112+
@RequestMapping(path = "/user")
113+
public class UserWebFluxController {
114+
115+
@Autowired
116+
private UserHandler userHandler;
117+
118+
@GetMapping(path = "/{id}")
119+
public Mono<User> findUserById(@PathVariable("id") Long id) {
120+
return userHandler.findUserById(id);
121+
}
122+
123+
@GetMapping()
124+
public Flux<User> findAllUser() {
125+
return userHandler.findAllUser();
126+
}
127+
128+
@PostMapping()
129+
public Mono<Long> saveUser(@RequestBody User user) {
130+
return userHandler.save(user);
131+
}
132+
133+
@PutMapping()
134+
public Mono<Long> modifyUser(@RequestBody User user) {
135+
return userHandler.modifyUser(user);
136+
}
137+
138+
@DeleteMapping(path = "/{id}")
139+
public Mono<Long> deleteUser(@PathVariable(value = "id") Long id) {
140+
return userHandler.deleteUser(id);
141+
}
142+
}
143+
```
144+
145+
146+
6. 测试步骤
147+
1. POST 请求 → 新增
148+
```text
149+
请求地址:http://localhost:8080/user
150+
请求参数如下:(raw/json)
151+
{
152+
"username": "Drew",
153+
"age": 25,
154+
"sex": "男",
155+
"realName": "杜鲁",
156+
"password": 123
157+
}
158+
```
159+
2. PUT 请求 → 修改
160+
```text
161+
请求地址:http://localhost:8080/user
162+
请求参数如下:(raw/json)
163+
{
164+
"username": "DrewPlus",
165+
"age": 23,
166+
"sex": "男",
167+
"realName": "杜鲁PLUS",
168+
"password": 123
169+
}
170+
```
171+
3. GET 请求 👉 查询
172+
```text
173+
(1)请求地址:http://localhost:8080/user/3
174+
返回数据如下:
175+
{
176+
"id": 3,
177+
"username": "Drew",
178+
"realName": "杜鲁",
179+
"password": "123",
180+
"age": 25,
181+
"sex": "男"
182+
}
183+
(2)请求所有:http://localhost:8080/user
184+
返回数据如下:
185+
[
186+
{
187+
"id": 1,
188+
"username": "Drew Plus",
189+
"realName": "杜鲁",
190+
"password": "123",
191+
"age": 25,
192+
"sex": "男"
193+
},
194+
{
195+
"id": 2,
196+
"username": "Drew",
197+
"realName": "杜鲁",
198+
"password": "123",
199+
"age": 25,
200+
"sex": "男"
201+
},
202+
{
203+
"id": 3,
204+
"username": "Drew",
205+
"realName": "杜鲁",
206+
"password": "123",
207+
"age": 25,
208+
"sex": "男"
209+
}
210+
]
211+
```
212+
4. DELETE 请求 → 删除
213+
```text
214+
请求地址:http://localhost:8080/user/1
215+
返回数据:1 (即用户ID)
216+
```
217+
218+
# 三、延伸阅读
219+

springboot-webflux/pom.xml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>edu.study.module</groupId>
6+
<artifactId>springboot-webflux</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>springboot-webflux</name>
9+
<description>RESTFul风格配合WebFlux project for Spring Boot</description>
10+
11+
<properties>
12+
<java.version>1.8</java.version>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15+
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-data-rest</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-webflux</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.alibaba</groupId>
33+
<artifactId>fastjson</artifactId>
34+
<version>1.2.72</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.apache.commons</groupId>
38+
<artifactId>commons-lang3</artifactId>
39+
<version>3.1</version>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.projectlombok</groupId>
44+
<artifactId>lombok</artifactId>
45+
<optional>true</optional>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-test</artifactId>
50+
<scope>test</scope>
51+
<exclusions>
52+
<exclusion>
53+
<groupId>org.junit.vintage</groupId>
54+
<artifactId>junit-vintage-engine</artifactId>
55+
</exclusion>
56+
</exclusions>
57+
</dependency>
58+
<dependency>
59+
<groupId>io.projectreactor</groupId>
60+
<artifactId>reactor-test</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
</dependencies>
64+
65+
<dependencyManagement>
66+
<dependencies>
67+
<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-dependencies</artifactId>
70+
<version>${spring-boot.version}</version>
71+
<type>pom</type>
72+
<scope>import</scope>
73+
</dependency>
74+
</dependencies>
75+
</dependencyManagement>
76+
77+
<build>
78+
<plugins>
79+
<plugin>
80+
<groupId>org.apache.maven.plugins</groupId>
81+
<artifactId>maven-compiler-plugin</artifactId>
82+
<version>3.8.1</version>
83+
<configuration>
84+
<source>1.8</source>
85+
<target>1.8</target>
86+
<encoding>UTF-8</encoding>
87+
</configuration>
88+
</plugin>
89+
<plugin>
90+
<groupId>org.springframework.boot</groupId>
91+
<artifactId>spring-boot-maven-plugin</artifactId>
92+
<version>2.3.7.RELEASE</version>
93+
<configuration>
94+
<mainClass>edu.study.module.springbootwebflux.SpringbootWebfluxApplication</mainClass>
95+
</configuration>
96+
<executions>
97+
<execution>
98+
<id>repackage</id>
99+
<goals>
100+
<goal>repackage</goal>
101+
</goals>
102+
</execution>
103+
</executions>
104+
</plugin>
105+
</plugins>
106+
</build>
107+
108+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package edu.study.module.springbootwebflux;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootWebfluxApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootWebfluxApplication.class, args);
11+
}
12+
13+
}

0 commit comments

Comments
 (0)