引入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. R语言与概率统计(六) 主成分分析 因子分析

    超高维度分析,N*P的矩阵,N为样本个数,P为指标,N<<P PCA:抓住对y对重要的影响因素 主要有三种:PCA,因子分析,回归方程+惩罚函数(如LASSO) 为了降维,用更少的变量解决 ...

  2. 手写web框架之开发一个类加载器

    ackage io.renren.common; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUti ...

  3. Java数组(3):创建测试数据

    有时我们需要使用数组批量创建测试数据,接下来通过以下4点来举例. (1) 使用Arrays.fill()填充数据 (2) 使用Random类中JDK1.8提供的新方法用来生成随机数 (3) 一个随机数 ...

  4. System.arraycopy() 数组复制方法

    一.深度复制和浅度复制的区别    Java数组的复制操作可以分为深度复制和浅度复制,简单来说深度复制,可以将对象的值和对象的内容复制;浅复制是指对对象引用的复制. 二.System.arraycop ...

  5. C#中的属性-Property

    C#的属性一直都有用,但具体了解的不是很深,而且一些注意事项也没有太在意过,糊里糊涂的用着.这两天看了C#的书专门学习了一下属性,这才知道,原来属性也有这么多东西~ ~今天记录一下,算是对学习的一个检 ...

  6. Shell脚本无限调用

    #! /bin/bash # this shell can run endlessfully echo "i love you ! " sh ./run 通过echo来显示了无限调 ...

  7. 软件测试第4周小组作业:WordCount优化

    一.基本任务:代码编写+单元测试 1.Github地址: https://github.com/Wegnery/New_WordCount 2.PSP2.1表格 PSP2.1 PSP阶段 预估耗时 ( ...

  8. highway network及mnist数据集测试

    先说结论:没经过仔细调参,打不开论文所说代码链接(fq也没打开),结果和普通卷积网络比较没有优势.反倒是BN对网络起着非常重要的作用,达到了99.17%的测试精度(训练轮数还没到过拟合). 论文为&l ...

  9. python录音并调用百度语音识别接口

    #!/usr/bin/env python import requests import json import base64 import pyaudio import wave import os ...

  10. 随笔--第一次使用crontab linux选择编辑器问题

    第一次使用crontab 时,会出现:no crontab for root - using an empty one “Select a editor ......”下面有几个选项,就是叫你选择编辑 ...