引入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. docker 搭建私有云盘 Seafile

    缘起 现如今各种云存储服务其实挺多的,国外有经典的DropBox.Google Drive.微软的OneDrive等,国内也有可以免费使用的各种云. 那么为什么想要搭建私有云存储呢?主要是本着“自己的 ...

  2. Centos安装openjdk

    转载自:https://blog.csdn.net/youzhouliu/article/details/51183115 openjdk在linux各个平台下安装源中可以找到. 命令查找安装源中有什 ...

  3. Linux命令集锦:scp命令

    scp命令用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的,可能会稍微影响一下速度.当你服务器硬盘变为只读 read on ...

  4. iptable的四表五链

    iptable的概念中有四张表,五条链. 四张表是: filter表——过滤数据包 Nat表——用于网络地址转换(IP.端口) Mangle表——修改数据包的服务类型.TTL.并且可以配置路由实现QO ...

  5. Python 面向对象--继承,实现,依赖,关联,聚合,组合

    一. 继承 继承指的是子类继承父类除私有内容以外的其他所有内容, 并且子类具有增加自己新内容的能力. 举例说明: class Animal: print("吃是动物的本能") cl ...

  6. .net 开源 FTP 组件 edtFTPnet

    edtFTPnet官方网站:http://www.enterprisedt.com/products/edtftpnet/ 目前最新版本为2.2.3,下载后在bin目录中找到edtFTPnet.dll ...

  7. axios中的qs介绍

    首先qs是一个npm仓库所管理的包,可通过npm install qs命令进行安装. 地址: https://www.npmjs.com/package/qs qs.parse().qs.string ...

  8. Java之属性和普通方法

    一.定义类 上一节讲了很多深奥的理论,那么这节我们就得实践一下,先简单描述一下我们的实体世界:有一个学生小明,那么这个学生就是一个对象,这个对象有哪些属性和方法呢,我们可以先简单抽象一下,属性有(姓名 ...

  9. mapreduce案例:获取PI的值

    mapreduce案例:获取PI的值 * content:核心思想是向以(0,0),(0,1),(1,0),(1,1)为顶点的正方形中投掷随机点. * 统计(0.5,0.5)为圆心的单位圆中落点占总落 ...

  10. discriminator 鉴别器

    在特定的情况下使用不同的pojo进行关联, 鉴别器元素就是被设计来处理这个情况的.鉴别器非常容易理解,因为它的表现很像 Java 语言中的 switch 语句:discriminator 标签常用的两 ...