Spring Boot 2.0 - WebFlux With MongoDB


声明:本文转载自https://my.oschina.net/u/3677020/blog/1579169,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

原文链接 http://www.spring4all.com/article/239

1、理论知识

Spring Boot 2.0 - WebFlux framework

2、基于 Spring Boot 2.0 的实践

① 在 docker 上运行 MongoDB

首先,获取 MongoDB 的镜像:

$ docker pull mongo 

然后启动 MongoDB 容器

$ docker run -d --name any-mongo -p 27017:27017 mongo 

② 构建 Spring Boot 2.0 WebFlux 运行环境

首先,在 IDEA 上新建 Maven 工程,pom.xml 文件内容如下:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>      <groupId>com.anoy</groupId>     <artifactId>webflux</artifactId>     <version>1.0-SNAPSHOT</version>      <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>2.0.0.M3</version>     </parent>      <dependencies>          <!-- ❤️不要添加 spring-boot-starter-web  -->         <!-- webflux 支持  -->         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-webflux</artifactId>             <exclusions>                 <!-- 移除 tomcat -->                 <exclusion>                     <groupId>org.springframework.boot</groupId>                     <artifactId>spring-boot-starter-tomcat</artifactId>                 </exclusion>                 <!-- 移除默认 logging -->                 <exclusion>                     <groupId>org.springframework.boot</groupId>                     <artifactId>spring-boot-starter-logging</artifactId>                 </exclusion>             </exclusions>         </dependency>          <!-- ❤️undertow -->         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-undertow</artifactId>         </dependency>                  <!-- ❤️响应式 MongoDB 支持 -->         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>         </dependency>          <!-- 代码简化 -->         <dependency>             <groupId>org.projectlombok</groupId>             <artifactId>lombok</artifactId>         </dependency>          <!-- 日志 Log4j2 -->         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-log4j2</artifactId>         </dependency>      </dependencies>      <repositories>         <repository>             <id>spring-milestones</id>             <name>Spring Milestones</name>             <url>https://repo.spring.io/libs-milestone</url>             <snapshots>                 <enabled>false</enabled>             </snapshots>         </repository>     </repositories>      <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build>  </project> 

然后,配置 Log4j2,参考如下文章: Spring Boot Log4j2 日志性能之巅

接着,配置 MongoDB,在 application.yml 添加如下内容:

spring:   data:     mongodb:       host: localhost       port: 27017 

> 小技巧:IDEA 有 MongoDB 的插件,可以方便的查看 MongoDB 里面的数据,插件名字:Mongo Plugin

Mongo Plugin

添加 Spring Boot 启动类:

package com.anoy;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;  @SpringBootApplication public class Application {      public static void main(String[] args) {         SpringApplication.run(Application.class);     }  }  

添加 Person 类:

package com.anoy.bean;  import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.annotation.Id;  @Data @AllArgsConstructor @NoArgsConstructor public class Person {      @Id     private Long id;      private String username;  }  

添加 PersonRepository,负责操作 MongoDB:

package com.anoy.repository;  import com.anoy.bean.Person; import org.springframework.context.annotation.Primary; import org.springframework.data.mongodb.repository.ReactiveMongoRepository; import org.springframework.stereotype.Repository;  @Repository @Primary public interface PersonRepository extends ReactiveMongoRepository<Person, Long>{  }  

添加 PersonController , 负责路由:

package com.anoy.controller;  import com.anoy.bean.Person; import com.anoy.repository.PersonRepository; import lombok.AllArgsConstructor; import org.reactivestreams.Publisher; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono;  @RestController @AllArgsConstructor public class PersonController {      private final PersonRepository personRepository;      /**      * 正常 MVC 模式      */     @GetMapping("/")     public String hello(){         return "hello!";     }      /**      * 新增一个 Person      */     @PostMapping("/person")     public Mono<Void> add(@RequestBody Publisher<Person> person){         return personRepository.insert(person).then();     }      /**      * 根据 ID 查询 Person      */     @GetMapping("/person/{id}")     public Mono<Person> getById(@PathVariable Long id){         return personRepository.findById(id);     }      /**      * 查询所有 Person      */     @GetMapping("/person/list")     public Flux<Person> list(){         return personRepository.findAll();     }      /**      * 删除指定 Person      */     @DeleteMapping("/person/{id}")     public Mono<Void> delete(@PathVariable Long id){         return personRepository.deleteById(id).then();     }  } 

③ 测试功能

插入数据

MongoDB 中的数据

查询所有

根据 ID 查询

根据 ID 删除

本文发表于2017年11月24日 10:33
(c)注:本文转载自https://my.oschina.net/u/3677020/blog/1579169,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 3170 讨论 1 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1