Skip to content

Commit 0cf03f6

Browse files
author
YunaiV
committed
spring webflux 示例
1 parent 64396c0 commit 0cf03f6

File tree

9 files changed

+337
-0
lines changed

9 files changed

+337
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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-01</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+
<!-- 方便等会写单元测试 -->
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-test</artifactId>
27+
<scope>test</scope>
28+
</dependency>
29+
30+
</dependencies>
31+
32+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.config;
2+
3+
import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.util.StringUtils;
7+
import org.springframework.web.reactive.function.server.*;
8+
import reactor.core.publisher.Mono;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.UUID;
13+
14+
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
15+
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
16+
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
17+
18+
@Configuration
19+
public class WebFluxRouteConfiguration {
20+
21+
@Bean
22+
public RouterFunction<ServerResponse> userListRouterFunction() {
23+
return RouterFunctions.route(RequestPredicates.GET("/users2/list"),
24+
new HandlerFunction<ServerResponse>() {
25+
26+
@Override
27+
public Mono<ServerResponse> handle(ServerRequest request) {
28+
// 查询列表
29+
List<UserVO> result = new ArrayList<>();
30+
result.add(new UserVO().setId(1).setUsername("yudaoyuanma"));
31+
result.add(new UserVO().setId(2).setUsername("woshiyutou"));
32+
result.add(new UserVO().setId(3).setUsername("chifanshuijiao"));
33+
// 返回列表
34+
return ServerResponse.ok().bodyValue(result);
35+
}
36+
37+
});
38+
}
39+
40+
@Bean
41+
public RouterFunction<ServerResponse> userGetRouterFunction() {
42+
return RouterFunctions.route(RequestPredicates.GET("/users2/get"),
43+
new HandlerFunction<ServerResponse>() {
44+
45+
@Override
46+
public Mono<ServerResponse> handle(ServerRequest request) {
47+
// 获得编号
48+
Integer id = request.queryParam("id")
49+
.map(s -> StringUtils.isEmpty(s) ? null : Integer.valueOf(s)).get();
50+
// 查询用户
51+
UserVO user = new UserVO().setId(id).setUsername(UUID.randomUUID().toString());
52+
// 返回列表
53+
return ServerResponse.ok().bodyValue(user);
54+
}
55+
56+
});
57+
}
58+
59+
@Bean
60+
public RouterFunction<ServerResponse> demoRouterFunction() {
61+
return route(GET("/users2/demo"), request -> ok().bodyValue("demo"));
62+
}
63+
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.controller;
2+
3+
import cn.iocoder.springboot.lab27.springwebflux.dto.UserAddDTO;
4+
import cn.iocoder.springboot.lab27.springwebflux.dto.UserUpdateDTO;
5+
import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO;
6+
import org.springframework.web.bind.annotation.*;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.publisher.Mono;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.UUID;
13+
14+
/**
15+
* 用户 Controller
16+
*/
17+
@RestController
18+
@RequestMapping("/users")
19+
public class UserController {
20+
21+
/**
22+
* 查询用户列表
23+
*
24+
* @return 用户列表
25+
*/
26+
@GetMapping("/list")
27+
public Flux<UserVO> list() {
28+
// 查询列表
29+
List<UserVO> result = new ArrayList<>();
30+
result.add(new UserVO().setId(1).setUsername("yudaoyuanma"));
31+
result.add(new UserVO().setId(2).setUsername("woshiyutou"));
32+
result.add(new UserVO().setId(3).setUsername("chifanshuijiao"));
33+
// 返回列表
34+
return Flux.fromIterable(result);
35+
}
36+
37+
/**
38+
* 获得指定用户编号的用户
39+
*
40+
* @param id 用户编号
41+
* @return 用户
42+
*/
43+
@GetMapping("/get")
44+
public Mono<UserVO> get(@RequestParam("id") Integer id) {
45+
// 查询用户
46+
UserVO user = new UserVO().setId(id).setUsername(UUID.randomUUID().toString());
47+
// 返回
48+
return Mono.just(user);
49+
}
50+
51+
/**
52+
* 添加用户
53+
*
54+
* @param addDTO 添加用户信息 DTO
55+
* @return 添加成功的用户编号
56+
*/
57+
@PostMapping("add")
58+
public Mono<Integer> add(UserAddDTO addDTO) {
59+
// 插入用户记录,返回编号
60+
Integer returnId = UUID.randomUUID().hashCode();
61+
// 返回用户编号
62+
return Mono.just(returnId);
63+
}
64+
65+
/**
66+
* 更新指定用户编号的用户
67+
*
68+
* @param updateDTO 更新用户信息 DTO
69+
* @return 是否修改成功
70+
*/
71+
@PostMapping("/update")
72+
public Mono<Boolean> update(UserUpdateDTO updateDTO) {
73+
// 更新用户记录
74+
Boolean success = true;
75+
// 返回更新是否成功
76+
return Mono.just(success);
77+
}
78+
79+
/**
80+
* 删除指定用户编号的用户
81+
*
82+
* @param id 用户编号
83+
* @return 是否删除成功
84+
*/
85+
@PostMapping("/delete") // URL 修改成 /delete ,RequestMethod 改成 DELETE
86+
public Mono<Boolean> delete(@RequestParam("id") Integer id) {
87+
// 删除用户记录
88+
Boolean success = false;
89+
// 返回是否更新成功
90+
return Mono.just(success);
91+
}
92+
93+
}
Lines changed: 35 additions & 0 deletions
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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.dto;
2+
3+
public class UserUpdateDTO {
4+
5+
/**
6+
* 编号
7+
*/
8+
private Integer id;
9+
/**
10+
* 账号
11+
*/
12+
private String username;
13+
/**
14+
* 密码
15+
*/
16+
private String password;
17+
18+
public Integer getId() {
19+
return id;
20+
}
21+
22+
public UserUpdateDTO setId(Integer id) {
23+
this.id = id;
24+
return this;
25+
}
26+
27+
public String getUsername() {
28+
return username;
29+
}
30+
31+
public UserUpdateDTO setUsername(String username) {
32+
this.username = username;
33+
return this;
34+
}
35+
36+
public String getPassword() {
37+
return password;
38+
}
39+
40+
public UserUpdateDTO setPassword(String password) {
41+
this.password = password;
42+
return this;
43+
}
44+
45+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.vo;
2+
3+
/**
4+
* 用户 VO
5+
*/
6+
public class UserVO {
7+
8+
/**
9+
* 编号
10+
*/
11+
private Integer id;
12+
/**
13+
* 账号
14+
*/
15+
private String username;
16+
17+
public Integer getId() {
18+
return id;
19+
}
20+
21+
public UserVO setId(Integer id) {
22+
this.id = id;
23+
return this;
24+
}
25+
26+
public String getUsername() {
27+
return username;
28+
}
29+
30+
public UserVO setUsername(String username) {
31+
this.username = username;
32+
return this;
33+
}
34+
35+
}

lab-27/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-27</artifactId>
13+
<packaging>pom</packaging>
14+
<modules>
15+
<module>lab-27-webflux-01</module>
16+
</modules>
17+
18+
19+
</project>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<module>lab-24</module>
3636
<module>lab-25</module>
3737
<module>lab-26</module>
38+
<module>lab-27</module>
3839
</modules>
3940

4041

0 commit comments

Comments
 (0)