【SpringBoot】集成 Web Flux
2018-06-18 00:18:21来源:未知 阅读 ()
前言:
必需学会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; }); } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
- Spring WebFlux 学习笔记 - (一) 前传:学习Java 8 Stream Ap 2020-06-11
- nacos~配置中心功能~springboot的支持 2020-06-10
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash