【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 连接打开后如下 ...
随机推荐
- JAVA语言编程思维入门
Java语言是一门强数据类型语言,也就是所有的数据有自己的数据类型,不能搞混淆.比如整数int 字符串String 不能用int a="字符串123";这样写是错的,因为数据类型不 ...
- Java中的集合框架-Collection(一)
一,Collection接口 在日常的开发工作中,我们经常使用数组,但是数组是有很多的局限性的,比如:数组大小固定后不可修改,只能存储基本类型的值等等. 基于数组的这些局限性,Java框架就产生了用于 ...
- 01 Oracle分区索引
Oracle分区索引 索引与表类似,也可以分区: 分区索引分为两类: Locally partitioned index(局部分区索引) Globally partitioned index(全局 ...
- SQL语句group by 与order by 执行顺序引发的一场“内斗”
直入主题!看看下面这SQL会不会报错?如果报错应该是什么错误! --说明:黑色字体都是列SELECT application_id, index_num, num, amount FROM `cred ...
- CUDA 版本,显卡驱动,Ubuntu版本,GCC版本之间的对应关系
- 在mac上使用tar.gz安装mysql
官方: download: https://dev.mysql.com/downloads/mysql/ mysql参考文档:https://dev.mysql.com/doc/ 环境: macOS ...
- 004---IO模型
io模型 同步.异步.阻塞.非阻塞概念 同步:发出一个功能调用时,在没有得到结果之前,该调用就不会返回,原地等待 异步:相反,不需要等待 阻塞:调用结果返回之前,当前线程会被挂起,如io操作,只有在得 ...
- mysql底层实现
MySQL 的常用引擎 1. InnoDB InnoDB 的存储文件有两个,后缀名分别是 .frm 和 .idb,其中 .frm 是表的定义文件,而 idb 是数据文件. InnoDB 中存在表锁和行 ...
- scala (2) while 和变量
(1)在scala中声明变量有两个关键字,val和var val: 是不可变的,即声明了变量不能再进行更改,类似于java中的final var: 是可变的,即可以重新对其赋值 声明变量的通用格式: ...
- PAT (Advanced Level) Practice 1003 Emergency
思路:用深搜遍历出所有可达路径,每找到一条新路径时,对最大救援人数和最短路径数进行更新. #include<iostream> #include<cstdio> #includ ...