使用 Spring Boot WebFlux 构建Rest服务实验手册(一)

Spring WebFlux 是 Spring 5.0 推出的新的Web框架,主要是使用新的非阻塞(NIO)编程模型提高在某些场景下Web服务性能。本实验手册给学员演示如何使用该框架。

构建项目

构建一个新的 maven 项目,

groupId: cn.com.hohistar.spbt
artifactId: webflux-starter

然后将 pom.xml 文件修改为以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.com.hohistar.spbt</groupId>
<artifactId>webflux-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webflux-starter</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-test-autoconfigure-r2dbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-bom-r2dbc</artifactId>
<version>0.1.0.M3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>

新建Model类

新建名为: cn.com.hohistar.spbt.webfluxstarter.model 的包,然后在包中新建一个名为 Todo 的类, 内容为:

1
2
3
4
5
6
7
8
9
10
11

@Data
@AllArgsConstructor
public class Todo {

private Integer id;

private String title;

private String desc;
}

新建Api接口

新建名为: cn.com.hohistar.spbt.webfluxstarter.api 的包,然后在包中新建名为: TodoApi 的类, 内容为:

1
2
3
4
5
6
7
8
9
10
11
@RestController
public class TodoFluxApi {


@GetMapping("/api/todo")
public Flux<Todo> getAllTodos() {

Flux<Todo> res = Flux.fromArray(new Todo[] { new Todo(1, "todo1", "desc1"), new Todo(2, "todo2", "desc2")});
return res;
}
}

新建启动类

在包: cn.com.hohistar.spbt.webfluxstarter 中新建一个名为: WebfluxStarterApplication 的类,内容为:

1
2
3
4
5
6
7
8
@SpringBootApplication
public class WebfluxStarterApplication {

public static void main(String[] args) {

SpringApplication.run(WebfluxStarterApplication.class, args);
}
}

运行程序

启动程序,运行 WebfluxStarterApplication, 将看到系统出现类似如下的反馈:

1
2
3
4
5
6
7
8
9
10
11
12
13
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)

2019-12-13 08:35:53.692 INFO 47409 --- [ main] c.c.h.s.w.WebfluxStarterApplication : Starting WebfluxStarterApplication on JinideMacBook-Pro.local with PID 47409 (/Users/jini/Study/Spring/springboot/webflux-starter/target/classes started by jini in /Users/jini/Study/Spring/springboot/webflux-starter)
2019-12-13 08:35:53.696 INFO 47409 --- [ main] c.c.h.s.w.WebfluxStarterApplication : No active profile set, falling back to default profiles: default
2019-12-13 08:35:55.522 INFO 47409 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
2019-12-13 08:35:55.551 INFO 47409 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 22ms. Found 0 R2DBC repository interfaces.
2019-12-13 08:35:57.056 INFO 47409 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
2019-12-13 08:35:57.061 INFO 47409 --- [ main] c.c.h.s.w.WebfluxStarterApplication : Started WebfluxStarterApplication in 4.58 seconds (JVM running for 6.573)

注意:这里已经不再是启动 tomcat, 而是直接使用 Netty库了

然后在浏览器中访问:

1
http://localhost:8080/api/todo

可以看到浏览器中显示:

1
[{"id":1,"title":"todo1","desc":"desc1"},{"id":2,"title":"todo2","desc":"desc2"}]

本文标题:使用 Spring Boot WebFlux 构建Rest服务实验手册(一)

文章作者:Morning Star

发布时间:2019年12月04日 - 17:12

最后更新:2021年04月16日 - 15:04

原始链接:https://www.mls-tech.info/java/springboot-webflux-practice-01/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。