使用 Spring Boot 2.0 + WebFlux 实现 RESTful API功能

2025-05-29 0 34

概述

什么是 spring webflux, 它是一种异步的, 非阻塞的, 支持背压(back pressure)机制的web 开发框架. 要深入了解 spring webflux, 首先要了知道 reactive stream . 另一种编程姿势, 和命令式编程相对的姿势.

webflux 支持两种编程风(姿)格(势)

  • 使用 @controller 这种基于注解的姿势, 与sring mvc的姿势相同
  • 基于java 8 lambda的函数式编程风格

注意: 上面只是两种编程的姿势, 和"普通话和重庆话都是中国话"是一个道理. 我们公司也有外地的, 对他我说普通话, 对本地同事说重庆话. 这叫多态

创建项目

通过http://start.spring.io创建项目骨架.

使用 Spring Boot 2.0 + WebFlux 实现 RESTful API功能

如果是手工配置, 需要添加spring的里程碑(milestone)仓库:

?

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
<repositories>

<repository>

<id>spring-snapshots</id>

<name>spring snapshots</name>

<url>https://repo.spring.io/snapshot</url>

<snapshots>

<enabled>true</enabled>

</snapshots>

</repository>

<repository>

<id>spring-milestones</id>

<name>spring milestones</name>

<url>https://repo.spring.io/milestone</url>

<snapshots>

<enabled>false</enabled>

</snapshots>

</repository>

</repositories>

<pluginrepositories>

<pluginrepository>

<id>spring-snapshots</id>

<name>spring snapshots</name>

<url>https://repo.spring.io/snapshot</url>

<snapshots>

<enabled>true</enabled>

</snapshots>

</pluginrepository>

<pluginrepository>

<id>spring-milestones</id>

<name>spring milestones</name>

<url>https://repo.spring.io/milestone</url>

<snapshots>

<enabled>false</enabled>

</snapshots>

</pluginrepository>

</pluginrepositories>

测试

列举所有用户

使用 Spring Boot 2.0 + WebFlux 实现 RESTful API功能

创建用户

使用 Spring Boot 2.0 + WebFlux 实现 RESTful API功能

获取单个用户

使用 Spring Boot 2.0 + WebFlux 实现 RESTful API功能

修改

使用 Spring Boot 2.0 + WebFlux 实现 RESTful API功能

删除

使用 Spring Boot 2.0 + WebFlux 实现 RESTful API功能

源码

demo-spring-boot-webflux-annotaion

总结

以上所述是小编给大家介绍的使用 spring boot 2.0 + webflux 实现 restful api功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:https://segmentfault.com/a/1190000012896148

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 使用 Spring Boot 2.0 + WebFlux 实现 RESTful API功能 https://www.kuaiidc.com/112567.html

相关文章

发表评论
暂无评论