【SpringBoot】集成 Web Flux
程序员文章站
2022-04-23 08:05:03
必需学会SpringBoot基础知识 Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is ......
前言:
必需学会SpringBoot基础知识
简介:
Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible.
工具:
JDK8
apache-maven-3.5.2
IntelliJ IDEA 2018.1.3 x64
(1)新建一个springboot工程
(2)pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
(3)建立实体 User (简化不写了)
(4)建立 repository
package com.lwc.repository; import com.lwc.pojo.User; import org.springframework.stereotype.Repository; import java.util.Collection; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicInteger; /** * @author eddie.lee * @Package com.lwc.repository * @ClassName UserRepository * @description this is dao * @date created in 2018-06-07 21:36 * @modified by */ @Repository public class UserRespository { /** * 使用内存方式存储 ===》 Map */ private ConcurrentMap<Integer, User> repository = new ConcurrentHashMap<>(); /** * id生成 */ private final static AtomicInteger idGenerator = new AtomicInteger(); /** * 保护用户对象 * * @param user * @return 如果保存成功,返回true,否则,返回false */ public boolean save(User user) { // id 从 1 开始 Integer id = idGenerator.incrementAndGet(); // 设置主键 user.setId(id); return repository.put(id, user) == null; } /** * 返回所有用户列表 * * @return */ public Collection<User> findAll(){ return repository.values(); } }
(5)建立控制层
package com.lwc.controller; import com.lwc.pojo.User; import com.lwc.repository.UserRespository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * @author eddie.lee * @Package com.lwc.controller * @ClassName UserController * @description * @date created in 2018-06-07 21:44 * @modified by */ @RestController public class UserController { private final UserRespository userRespository; @Autowired public UserController(UserRespository userRespository) { this.userRespository = userRespository; } @PostMapping("/person/save") public User save(@RequestParam("name") String name) { User user = new User(); user.setName(name); if (userRespository.save(user)) { System.out.printf("用户对象: %s 保存成功! ", user); System.out.println(" "); } return user; } }
(6)PostMan测试,插入接口。
http://localhost:8080/person/save?name=zhangsan2
{
"id": 1,
"name": "zhangsan2"
}
(7)使用 Web Flux , Spring 5.0 后提出,优点是NIO
package com.lwc.config; import com.lwc.pojo.User; import com.lwc.repository.UserRespository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.Collection; /** * @author eddie.lee * @Package com.lwc.config * @ClassName RouterFunctionConfiguration * @description 路由器函数配置 * @date created in 2018-06-07 22:33 * @modified by */ @Configuration public class RouterFunctionConfiguration { // @Autowired // private UserRespository userRepository; @Bean @Autowired public RouterFunction<ServerResponse> personFindAll(UserRespository userRepository) { return RouterFunctions.route(RequestPredicates.GET("/person/find/all"), request -> { Collection<User> users = userRepository.findAll(); Flux<User> userFlux = Flux.fromIterable(users); Mono<ServerResponse> body = ServerResponse.ok().body(userFlux, User.class); return body; }); } }
下一篇: 法院旁听
推荐阅读
-
SpringBoot集成JWT实现权限认证
-
[springboot 开发单体web shop] 4. Swagger生成Javadoc
-
五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链
-
SpringBoot集成WebSocket长连接实际应用详解
-
Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置
-
小白的springboot之路(十二)、集成log4j2日志
-
springcloud Springboot vue.js Activiti6 前后分离 跨域 工作流 集成代码生成器 shiro权限
-
JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
-
Springboot Activiti6 工作流 集成代码生成器 shiro 权限 vue.js html 跨域 前后分离
-
springboot集成mybatisplus的方法