【SpringBoot】集成 Web Flux
前言:
必需学会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】集成 Web Flux的更多相关文章
- SpringBoot 集成Web
1,静态资源访问: 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: ...
- springBoot集成web service
转载大神: https://blog.csdn.net/u011410529/article/details/68063541?winzoom=1 https://blog.csdn.net/nr00 ...
- Shiro集成web环境[Springboot]-认证与授权
Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pag ...
- Shiro集成web环境[Springboot]-基础使用
Shiro集成web环境[Springboot] 1.shiro官网查找依赖的jar,其中shiro-ehcache做授权缓存时使用,另外还需要导入ehcache的jar包 <dependenc ...
- Springboot搭建web项目
最近因为项目需要接触了springboot,然后被其快速零配置的特点惊呆了.关于springboot相关的介绍我就不赘述了,大家自行百度google. 一.pom配置 首先,建立一个maven项目,修 ...
- springboot集成Actuator
Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...
- SpringBoot集成jsp
一.springBoot集成jsp: 1.修改pom文件 <!--集成jsp所需jar包--> <!--jsp页面使用jstl标签--> <dependency> ...
- springboot集成schedule(深度理解)
背景 在项目开发过程中,我们经常需要执行具有周期性的任务.通过定时任务可以很好的帮助我们实现. 我们拿常用的几种定时任务框架做一个比较: 从以上表格可以看出,Spring Schedule框架功能完善 ...
- Windows环境下springboot集成redis的安装与使用
一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...
随机推荐
- 404 Note Found 队-Alpha 事后诸葛亮
目录 设想和目标 计划 资源 变更管理 设计/实现 测试/发布 团队的角色,管理,合作 总结: 本小组和其他组的评分 分工和贡献分 全组讨论的照片 问题 第一组提问回答:爸爸饿了队 第二组提问回答:拖 ...
- 使用java代码将时间戳和时间互相转换
时间戳转时间: SimpleDateFormat simpleDateFormat = null; simpleDateFormat = new SimpleDateFormat("yyyy ...
- CentOS6安装各种大数据软件 第五章:Kafka集群的配置
相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...
- jdbc之连接Oracle的基本步骤
// 1.加载驱动程序 Class.forName("oracle.jdbc.driver.OracleDriver"); // 2.获取数据库连接 Connection conn ...
- AttributeError: module 'tensorflow' has no attribute 'enable_eager_execution'
Traceback (most recent call last): File "linear_regression_eager_api.py", line 15, in < ...
- Hbase(2)-HBase简介
一. HBase的特点 1. 海量存储 Hbase适合存储PB级别的海量数据,在PB级别的数据以及采用廉价PC存储的情况下,能在几十到百毫秒内返回数据.这与Hbase的极易扩展性息息相关.正式因为Hb ...
- WOT干货大放送:大数据架构发展趋势及探索实践分享
WOT大数据处理技术分会场,PingCAP CTO黄东旭.易观智库CTO郭炜.Mob开发者服务平台技术副总监林荣波.宜信技术研发中心高级架构师王东及商助科技(99Click)顾问总监郑泉五位讲师, ...
- Python学习 :常用模块(三)----- 日志记录
常用模块(三) 七.logging模块 日志中包含的信息应有正常的程序访问日志,还可能有错误.警告等信息输出 python的 logging 模块提供了标准的日志接口,你可以通过它存储各种格式的日志, ...
- ruby on rails 环境搭建(mac or ubuntu)
环境配置前操作 mac: app_store安装x-code ubuntu: 终端->配置文件->首选项->命令->以shell方式登录 安装RVM mac: $ ruby - ...
- python教程(三)·函数进阶(下)
下半部分果然很快到来,这次介绍函数的更高级用法,装饰器! 函数嵌套 先来说说函数嵌套,python中的函数是可以嵌套的,也就是说可以将一个函数放在另一个函数里面,比如: >>> de ...