引入maven文件

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>

创建配置文件

spring:
data:
mongodb:
host: 127.0.0.1
port: 27017
username: mickey
password: 123456
database: mongoTest

创建document实体类

@Data
@Document("person")
public class PersonEntity { @Id
private String id; private String userName; private String gender; /**
* 设置TTL,单位秒
*/
@Indexed(name = "idx_create_time", expireAfterSeconds = 10)
private Date createTime = new Date(); }

创建repository

@Repository
public interface PersonRepository extends ReactiveMongoRepository<PersonEntity,String> { /**
* 根据name查找Person
* @param name
* @return
*/
Flux<PersonEntity> findByUserName(String name);
}

编写controller

package com.xzsx.openapi.thirdparty.controller;

import com.xzsx.openapi.dto.MongoDBOutput;
import com.xzsx.openapi.thirdparty.entity.PersonEntity;
import com.xzsx.openapi.thirdparty.repository.PersonRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; /**
* @author J·K
* @Description: TODO
* @date 2019-04-22 10:19
*/
@RestController
public class IndexController
{
/**
* 可以使用
*/
@Autowired
private PersonRepository personRepository; @GetMapping("/save")
public Mono<PersonEntity> save(){
PersonEntity person = new PersonEntity();
person.setUserName("mickey");
person.setGender("male");
return personRepository.save(person);
} @GetMapping("/list")
public Flux<MongoDBOutput> list(){
Flux<MongoDBOutput> flux = personRepository.findAll().map(x->{
MongoDBOutput mongoDBOutput = new MongoDBOutput();
BeanUtils.copyProperties(x,mongoDBOutput);
return mongoDBOutput;
});
return flux;
} @GetMapping("/delete/{id}")
public Mono<String> delete(@PathVariable("id") String id){
//没有返回值
// personRepository.deleteById(id); //如果要操作数据,并返回一个Mono,这时候使用flatMap
//如果不操作数据,只是对数据做一个转换,使用map
return personRepository.findById(id)
.flatMap(x->
personRepository.deleteById(x.getId())
.then(Mono.just("ok")))
.defaultIfEmpty("not found");
} @GetMapping("/update/{id}")
public Mono<String> update(@PathVariable("id") String id){
return personRepository.findById(id)
.flatMap(x->{
x.setUserName("jack");
return personRepository.save(x);
})
.map(x->x.toString())
.defaultIfEmpty("error");
} @GetMapping("/find/{id}")
public Mono<PersonEntity> findById(@PathVariable("id") String id){
return personRepository.findById(id)
.map(x-> x)
.defaultIfEmpty(new PersonEntity());
} @GetMapping("/findByName/{name}")
public Flux<PersonEntity> findByName(@PathVariable("name") String name){
return personRepository.findByUserName(name)
.map(x-> x)
.defaultIfEmpty(new PersonEntity());
} @GetMapping(value = "/stream/findByName/{name}",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<PersonEntity> findByName1(@PathVariable("name") String name){
return personRepository.findByUserName(name)
.map(x-> x)
.defaultIfEmpty(new PersonEntity());
} public static void main(String[] args) {
String [] strs = {"1","2","3"};
// List<Integer> collect = Flux.fromArray(strs).map(x -> Integer.parseInt(x))
// .toStream().collect(Collectors.toList());
// System.out.println(collect); // Flux.fromArray(strs).map(x->Integer.parseInt(x))
// .subscribe(x->{
// System.out.println(x);
// }); Flux.fromArray(strs).map(x->{
throw new RuntimeException("error");
})
.subscribe(x->{
System.out.println(x);
},x->{
System.out.println("error");
}); } }

启动项目后,就可以按原来访问springboot的方式访问数据了

Spring Boot WebFlux整合mongoDB的更多相关文章

  1. Spring Boot WebFlux 集成 Mongodb 数据源操作

    WebFlux 整合 Mongodb 前言 上一讲用 Map 数据结构内存式存储了数据.这样数据就不会持久化,本文我们用 MongoDB 来实现 WebFlux 对数据源的操作. 什么是 MongoD ...

  2. Spring Boot WebFlux-03——WebFlux 整合 MongoDB

    第03课:WebFlux 整合 MongoDB 前言 上一课的内容讲解了用 Map 数据结构内存式存储了数据,这样数据就不会持久化,本文我们用 MongoDB 来实现 WebFlux 对数据源的操作. ...

  3. Spring Boot WebFlux 快速入门实践

    02:WebFlux 快速入门实践 Spring Boot 2.0 spring.io 官网有句醒目的话是: BUILD ANYTHING WITH SPRING BOOT Spring Boot ( ...

  4. Spring Boot (十四): 响应式编程以及 Spring Boot Webflux 快速入门

    1. 什么是响应式编程 在计算机中,响应式编程或反应式编程(英语:Reactive programming)是一种面向数据流和变化传播的编程范式.这意味着可以在编程语言中很方便地表达静态或动态的数据流 ...

  5. Spring Boot Security 整合 JWT 实现 无状态的分布式API接口

    简介 JSON Web Token(缩写 JWT)是目前最流行的跨域认证解决方案.JSON Web Token 入门教程 - 阮一峰,这篇文章可以帮你了解JWT的概念.本文重点讲解Spring Boo ...

  6. Spring boot Mybatis 整合(完整版)

    个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...

  7. Spring boot Mybatis 整合

    PS: 参考博客 PS: spring boot配置mybatis和事务管理 PS: Spring boot Mybatis 整合(完整版)   这篇博客里用到了怎样 生成 mybatis 插件来写程 ...

  8. spring boot 学习(二)spring boot 框架整合 thymeleaf

    spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...

  9. Spring Boot 应用系列 5 -- Spring Boot 2 整合logback

    上一篇我们梳理了Spring Boot 2 整合log4j2的配置过程,其中讲到了Spring Boot 2原装适配logback,并且在非异步环境下logback和log4j2的性能差别不大,所以对 ...

随机推荐

  1. 一百零二:CMS系统之sweetalert提示框和使用

    实现效果 css body.stop-scrolling { height: 100%; overflow: hidden; } .sweet-overlay { background-color: ...

  2. powerdesigner通过er图生成mysql执行文件

    PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...

  3. Servlet(1):基础概念/最简实例

    Servlet 生命周期(1) init()方法初始化Servlet对象  它在第一次创建Servlet时被调用,在后续每次不同用户请求时不再调用.(2) service()方法来处理客户端的请求  ...

  4. 连载一:RobotFramework+SeleniumWebdriver+RIDE的安装

    安装前说明: Robot Framework自动化测试框架+可视化编辑工具RIDE+Selenium2这是规范的webAPI. 一.通过下载安装包安装 1)RF 框架是基于 Python 语言的,所以 ...

  5. .Netcore 2.0 Ocelot Api网关教程(4)- 服务发现

    本文介绍Ocelot中的服务发现(Service Discovery),Ocelot允许指定一个服务发现提供器,之后将从中寻找下游服务的host和port来进行请求路由.关于服务发现的详细介绍请点击. ...

  6. 【VS开发】【C/C++开发】传递双重指针申请内存,典型用法

    传递双重指针申请内存,典型用法 指针参数是如何传递内存的? 如果函数的参数是一个指针,不要指望用该指针去申请动态内存.如下示例中,Test函数的语句GetMemory(str, 100)并没有使str ...

  7. 选择排序的Python代码实现

    对于a[0]~a[n]的数组, 默认a[i]最小,和后面的a[i+1]~a[n]进行比较,把最小的和a[i]交换位置,保证本次循环结束后a[i]是上一次未排序的数据中最小的 写法1 a=[12,2,2 ...

  8. Oracle - 子查询、TOP - N

    1 子查询 sql 中查询是可以嵌套的,一个查询的结果可以作为另外一个查询的条件.表. SELECT select_list FROM table WHERE expr operator (SELEC ...

  9. SQL Server 2019 Linux Docker 在主机上以其他非根用户的身份运行容器

    docker logs mssql2019SQL Server 2019 will run as non-root by default.This container is running as us ...

  10. position: sticky 防坑指南

    position: sticky 防坑指南:https://www.jianshu.com/p/e217905e8b87 今天在写小程序项目的时候碰到一个需求是要把轮播图下面的标签栏滑动到顶部后固定, ...