概述
什么是WebFlux
1、Spring5以后引入的响应式web框架
2、不依赖Servlet API,完全异步且无阻塞
3、通过Reactor实现Reactive Streams规范
4、能够提高系统的吞吐量和伸缩性
5、能够提供动态的HTML内容
增删改查案例
引入依赖
SpringBoot
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
webflux
org.springframework.boot
spring-boot-starter-webflux
准备实体类
User
模拟数据库
Map users = new HashMap();
@PostConstruct//依赖关系注入完成之后,执行初始化
public void init() throws Exception {
users.put(1L, new User(1L, "张大鹏", 28));
users.put(2L, new User(2L, "高翔", 2));
}
接口:新增数据
接口设计
方法:POST
路径:/user
入参:
id:用户id
name:用户名
age:年龄
响应:字符串
接口实现
@PostMapping("")
public Mono addUser(@RequestBody User user) {
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.getAge());
users.put(user.getId(), user);
return Mono.just(new ResponseEntity("添加成功", HttpStatus.CREATED));
}
接口测试
地址:POST
http://localhost:8080/user
参数:
{
"id": 3,
"name": "sss",
"age": 33
}
接口:删除数据
接口设计
方法:DELETE
路径:/user/
入参:路径参数
id:用户id
响应:字符串
接口实现
@DeleteMapping("/")
public Mono deleteUser(@PathVariable Long id) {
users.remove(id);
return Mono.just(new ResponseEntity("删除成功", HttpStatus.ACCEPTED));
}
接口测试
地址:DELETE
http://localhost:8080/user/1
接口:修改数据
接口设计
方法:PUT
路径:/user/
入参:
id:用户id,路径参数
name:用户名
age:年龄
响应:字符串
接口实现
@PutMapping("/")
public Mono putUser(@PathVariable Long id, @RequestBody User user) {
user.setId(id);
users.put(id, user);
return Mono.just(new ResponseEntity(user, HttpStatus.CREATED));
}
接口测试
地址:PUT
http://localhost:8080/user/1
参数:
{
"name": "xxx",
"age": 33
}
根据id查询用户
接口设计
方法:GET
路径:/user/
入参:
id:用户id,路径参数
响应:用户对象或空
接口实现
@GetMapping("/")
public Mono getUser(@PathVariable Long id) {
return Mono.justOrEmpty(users.get(id));
}
接口测试
地址:GET
http://localhost:8080/user/1
查询所有用户
接口设计
方法:GET
路径:/user
入参:无
响应:用户对象列表
接口实现
@GetMapping("")
public Flux getAll() {
return Flux.fromIterable(users.entrySet()
.stream()
.map(entry -> entry.getValue())
.collect(Collectors.toList()));
}
接口测试
地址:GET
http://localhost:8080/user
完整代码
pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.zhangdapeng520
hello_springboot1
0.0.1-SNAPSHOT
hello_springboot1
hello_springboot1
8
org.springframework.boot
spring-boot-starter-webflux
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
io.projectreactor
reactor-test
test
org.springframework.boot
spring-boot-maven-plugin
package com.zhangdapeng520.controller;
import com.zhangdapeng520.domain.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
@RestController
@RequestMapping(path = "/user")
public class HelloController {
Map users = new HashMap();
@PostConstruct//依赖关系注入完成之后,执行初始化
public void init() throws Exception {
users.put(1L, new User(1L, "张大鹏", 28));
users.put(2L, new User(2L, "高翔", 2));
}
@GetMapping("")
public Flux getAll() {
return Flux.fromIterable(users.entrySet().stream().map(entry -> entry.getValue()).collect(Collectors.toList()));
}
@GetMapping("/")
public Mono getUser(@PathVariable Long id) {
return Mono.justOrEmpty(users.get(id));
}
@PostMapping("")
public Mono addUser(@RequestBody User user) {
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.getAge());
users.put(user.getId(), user);
return Mono.just(new ResponseEntity("添加成功", HttpStatus.CREATED));
}
@PutMapping("/")
public Mono putUser(@PathVariable Long id, @RequestBody User user) {
user.setId(id);
users.put(id, user);
return Mono.just(new ResponseEntity(user, HttpStatus.CREATED));
}
@DeleteMapping("/")
public Mono deleteUser(@PathVariable Long id) {
users.remove(id);
return Mono.just(new ResponseEntity("删除成功", HttpStatus.ACCEPTED));
}
}
领取专属 10元无门槛券
私享最新 技术干货