Skip to content

Commit f6b91bb

Browse files
author
YunaiV
committed
增加 webflux 各种整合示例
1 parent 4636d54 commit f6b91bb

File tree

22 files changed

+759
-79
lines changed

22 files changed

+759
-79
lines changed

lab-27/lab-27-webflux-02/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
<artifactId>spring-boot-starter-webflux</artifactId>
2020
</dependency>
2121

22-
<!-- 引入 jackson 对 xml 的转换器,实现对 XML 的序列化 -->
23-
<dependency>
24-
<groupId>com.fasterxml.jackson.dataformat</groupId>
25-
<artifactId>jackson-dataformat-xml</artifactId>
26-
</dependency>
27-
2822
<!-- 方便等会写单元测试 -->
2923
<dependency>
3024
<groupId>org.springframework.boot</groupId>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.2.1.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>lab-27-webflux-elasticsearch</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对 Spring WebFlux 的自动化配置 -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-webflux</artifactId>
20+
<version>2.2.1.RELEASE</version>
21+
</dependency>
22+
23+
<!-- 自动化配置响应式的 Spring Data Elasticsearch -->
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
27+
</dependency>
28+
29+
<!-- 方便等会写单元测试 -->
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
</dependencies>
37+
38+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.iocoder.springboot.lab27.springwebflux;
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+
// System.setProperty("es.set.netty.runtime.available.processors", "false");
11+
SpringApplication.run(Application.class, args);
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.data.elasticsearch.repository.config.EnableReactiveElasticsearchRepositories;
5+
6+
@Configuration
7+
@EnableReactiveElasticsearchRepositories // 开启响应式的 Elasticsearch 的 Repository 的自动化配置
8+
public class ElasticsearchConfiguration {
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.controller;
2+
3+
import cn.iocoder.springboot.lab27.springwebflux.dao.UserRepository;
4+
import cn.iocoder.springboot.lab27.springwebflux.dataobject.UserDO;
5+
import cn.iocoder.springboot.lab27.springwebflux.dto.UserAddDTO;
6+
import cn.iocoder.springboot.lab27.springwebflux.dto.UserUpdateDTO;
7+
import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.*;
10+
import reactor.core.publisher.Flux;
11+
import reactor.core.publisher.Mono;
12+
13+
import java.util.Date;
14+
import java.util.Objects;
15+
import java.util.function.Function;
16+
17+
/**
18+
* 用户 Controller
19+
*/
20+
@RestController
21+
@RequestMapping("/users")
22+
public class UserController {
23+
24+
private static final UserDO USER_NULL = new UserDO();
25+
26+
@Autowired
27+
private UserRepository userRepository;
28+
29+
30+
/**
31+
* 查询用户列表
32+
*
33+
* @return 用户列表
34+
*/
35+
@GetMapping("/list")
36+
public Flux<UserVO> list() {
37+
// 返回列表
38+
return userRepository.findAll()
39+
.map(userDO -> new UserVO().setId(userDO.getId()).setUsername(userDO.getUsername()));
40+
}
41+
42+
/**
43+
* 获得指定用户编号的用户
44+
*
45+
* @param id 用户编号
46+
* @return 用户
47+
*/
48+
@GetMapping("/get")
49+
public Mono<UserVO> get(@RequestParam("id") Integer id) {
50+
// 返回
51+
return userRepository.findById(id)
52+
.map(userDO -> new UserVO().setId(userDO.getId()).setUsername(userDO.getUsername()));
53+
}
54+
55+
/**
56+
* 添加用户
57+
*
58+
* @param addDTO 添加用户信息 DTO
59+
* @return 添加成功的用户编号
60+
*/
61+
@PostMapping("add")
62+
public Mono<Integer> add(UserAddDTO addDTO) {
63+
// 查询用户
64+
Mono<UserDO> user = userRepository.findByUsername(addDTO.getUsername());
65+
66+
// 执行插入
67+
return user.defaultIfEmpty(USER_NULL) // 设置 USER_NULL 作为 null 的情况,否则 flatMap 不会往下走
68+
.flatMap(new Function<UserDO, Mono<Integer>>() {
69+
70+
@Override
71+
public Mono<Integer> apply(UserDO userDO) {
72+
if (userDO != USER_NULL) {
73+
// 返回 -1 表示插入失败。
74+
// 实际上,一般是抛出 ServiceException 异常。因为这个示例项目里暂时没做全局异常的定义,所以暂时返回 -1 啦
75+
return Mono.just(-1);
76+
}
77+
// 将 addDTO 转成 UserDO
78+
userDO = new UserDO().setId((int) (System.currentTimeMillis() / 1000)) // 使用当前时间戳的描述,作为 ID 。
79+
.setUsername(addDTO.getUsername())
80+
.setPassword(addDTO.getPassword())
81+
.setCreateTime(new Date());
82+
// 插入数据库
83+
return userRepository.save(userDO).map(UserDO::getId);
84+
}
85+
86+
});
87+
}
88+
89+
/**
90+
* 更新指定用户编号的用户
91+
*
92+
* @param updateDTO 更新用户信息 DTO
93+
* @return 是否修改成功
94+
*/
95+
@PostMapping("/update")
96+
public Mono<Boolean> update(UserUpdateDTO updateDTO) {
97+
// 查询用户
98+
Mono<UserDO> user = userRepository.findById(updateDTO.getId());
99+
100+
// 执行更新
101+
return user.defaultIfEmpty(USER_NULL) // 设置 USER_NULL 作为 null 的情况,否则 flatMap 不会往下走
102+
.flatMap(new Function<UserDO, Mono<Boolean>>() {
103+
104+
@Override
105+
public Mono<Boolean> apply(UserDO userDO) {
106+
// 如果不存在该用户,则直接返回 false 失败
107+
if (userDO == USER_NULL) {
108+
return Mono.just(false);
109+
}
110+
// 查询用户是否存在
111+
return userRepository.findByUsername(updateDTO.getUsername())
112+
.defaultIfEmpty(USER_NULL) // 设置 USER_NULL 作为 null 的情况,否则 flatMap 不会往下走
113+
.flatMap(new Function<UserDO, Mono<? extends Boolean>>() {
114+
115+
@Override
116+
public Mono<? extends Boolean> apply(UserDO usernameUserDO) {
117+
// 如果用户名已经使用(该用户名对应的 id 不是自己,说明就已经被使用了)
118+
if (usernameUserDO != USER_NULL && !Objects.equals(updateDTO.getId(), usernameUserDO.getId())) {
119+
return Mono.just(false);
120+
}
121+
// 执行更新
122+
userDO.setUsername(updateDTO.getUsername());
123+
userDO.setPassword(updateDTO.getPassword());
124+
return userRepository.save(userDO).map(userDO -> true); // 返回 true 成功
125+
}
126+
127+
});
128+
}
129+
130+
});
131+
}
132+
133+
/**
134+
* 删除指定用户编号的用户
135+
*
136+
* @param id 用户编号
137+
* @return 是否删除成功
138+
*/
139+
@PostMapping("/delete") // URL 修改成 /delete ,RequestMethod 改成 DELETE
140+
public Mono<Boolean> delete(@RequestParam("id") Integer id) {
141+
// 查询用户
142+
Mono<UserDO> user = userRepository.findById(id);
143+
144+
// 执行删除。这里仅仅是示例,项目中不要物理删除,而是标记删除
145+
return user.defaultIfEmpty(USER_NULL) // 设置 USER_NULL 作为 null 的情况,否则 flatMap 不会往下走
146+
.flatMap(new Function<UserDO, Mono<Boolean>>() {
147+
148+
@Override
149+
public Mono<Boolean> apply(UserDO userDO) {
150+
// 如果不存在该用户,则直接返回 false 失败
151+
if (userDO == USER_NULL) {
152+
return Mono.just(false);
153+
}
154+
// 执行删除
155+
return userRepository.deleteById(id).map(aVoid -> true); // 返回 true 成功
156+
}
157+
158+
});
159+
}
160+
161+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.dao;
2+
3+
import cn.iocoder.springboot.lab27.springwebflux.dataobject.UserDO;
4+
import org.springframework.data.elasticsearch.repository.ReactiveElasticsearchRepository;
5+
import reactor.core.publisher.Mono;
6+
7+
public interface UserRepository extends ReactiveElasticsearchRepository<UserDO, Integer> {
8+
9+
Mono<UserDO> findByUsername(String username);
10+
11+
}
12+
13+
//public interface UserRepository extends ElasticsearchRepository<UserDO, Integer> {
14+
//
15+
//// Mono<UserDO> findByUsername(String username);
16+
//
17+
//}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.dataobject;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.elasticsearch.annotations.Document;
5+
6+
import java.util.Date;
7+
8+
@Document(indexName = "user", // 索引名
9+
type = "user", // 类型。未来的版本即将废弃
10+
shards = 1, // 默认索引分区数
11+
replicas = 0, // 每个分区的备份数
12+
refreshInterval = "-1" // 刷新间隔
13+
)
14+
public class UserDO {
15+
16+
/**
17+
* ID 主键
18+
*/
19+
@Id
20+
private Integer id;
21+
/**
22+
* 账号
23+
*/
24+
private String username;
25+
/**
26+
* 密码
27+
*/
28+
private String password;
29+
/**
30+
* 创建时间
31+
*/
32+
private Date createTime;
33+
34+
public Integer getId() {
35+
return id;
36+
}
37+
38+
public UserDO setId(Integer id) {
39+
this.id = id;
40+
return this;
41+
}
42+
43+
public String getUsername() {
44+
return username;
45+
}
46+
47+
public UserDO setUsername(String username) {
48+
this.username = username;
49+
return this;
50+
}
51+
52+
public String getPassword() {
53+
return password;
54+
}
55+
56+
public UserDO setPassword(String password) {
57+
this.password = password;
58+
return this;
59+
}
60+
61+
public Date getCreateTime() {
62+
return createTime;
63+
}
64+
65+
public UserDO setCreateTime(Date createTime) {
66+
this.createTime = createTime;
67+
return this;
68+
}
69+
70+
@Override
71+
public String toString() {
72+
return "UserDO{" +
73+
"id=" + id +
74+
", username='" + username + '\'' +
75+
", password='" + password + '\'' +
76+
", createTime=" + createTime +
77+
'}';
78+
}
79+
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.dto;
2+
3+
/**
4+
* 用户添加 DTO
5+
*/
6+
public class UserAddDTO {
7+
8+
/**
9+
* 账号
10+
*/
11+
private String username;
12+
/**
13+
* 密码
14+
*/
15+
private String password;
16+
17+
public String getUsername() {
18+
return username;
19+
}
20+
21+
public UserAddDTO setUsername(String username) {
22+
this.username = username;
23+
return this;
24+
}
25+
26+
public String getPassword() {
27+
return password;
28+
}
29+
30+
public UserAddDTO setPassword(String password) {
31+
this.password = password;
32+
return this;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)