不知不觉,陆陆续续已经写过很多关于 WebFlux 教程了。不来不着急这写这一张的。但是最近有一个网友加了我的微信,反馈信息,问我:“如果进行 WebFlux + Mongo 的整合?”
于是,我就给他写了一个 demo。今天抽个时间,把这个 demo 发出来,希望能够帮助到更多人。
首先,我们可以创建一个 Maven 项目,如:webflux-mongo。
然后,pom.xml 配置文件的内容如下:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <!-- Spring Boot 响应式 MongoDB 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
然后,在 application.yml 配置 Mongo 的连接信息。
#mongodb的配置 spring: data: mongodb: host: 127.0.01 database: xttblog port: 27017 username: xttblog password: xttblog
然后是一个文档类:Xttblog。
public class Xttblog implements Serializable { private Long id; private String url; private String title; private String author; private String text; //...省略get/set }
然后是 XttblogRepository 类。
@Repository public interface XttblogRepository extends ReactiveMongoRepository<Xttblog, Long> { }
业务 XttblogHandler 类。
@Component public class XttblogHandler { @Autowired private XttblogRepository xttblogRepository; public Mono<Xttblog> save(Xttblog xttblog) { return xttblogRepository.save(xttblog); } public Flux<Xttblog> findAllXttblog() { return xttblogRepository.findAll().cache(); } public Mono<Xttblog> modifyXttblog(Xttblog xttblog) { Mono<Xttblog> resultMono = xttblogRepository.save(xttblog); return resultMono; } public Mono<Long> deleteXttblog(Long id) { xttblogRepository.deleteById(id); return Mono.create(monoSink -> monoSink.success(id)); } public Mono<Xttblog> findXttblogById(Long id) { // 从 MongoDB 中获取城市信息 Mono<Xttblog> xttblogMono = xttblogRepository.findById(id); return xttblogMono; } }
最后是 MongoController 类。
public class MongoController { @Autowired private XttblogHandler xttblogHandler; @GetMapping(value = "/{id}") public Mono<Xttblog> findXttblogById(@PathVariable("id") Long id) { return xttblogHandler.findXttblogById(id); } @GetMapping() public Flux<Xttblog> findAllXttblog() { return xttblogHandler.findAllXttblog(); } @PostMapping() public Mono<Xttblog> saveXttblog(@RequestBody Xttblog xttblog) { return xttblogHandler.save(xttblog); } @PutMapping() public Mono<Xttblog> modifyXttblog(@RequestBody Xttblog city) { return xttblogHandler.modifyXttblog(city); } @DeleteMapping(value = "/{id}") public Mono<Long> deleteXttblog(@PathVariable("id") Long id) { return xttblogHandler.deleteXttblog(id); } }
运行 MongoAppliction,代码如下:
@SpringBootApplication public class MongoAppliction { public static void main(String[] args) { SpringApplication.run(MongoAppliction.class, args); } }
至于,运行效果,我就不贴了。
更多关于 WebFlux 的教程内容,请阅读:WebFlux 教程。
: » WebFlux + Mongo 进行项目整合开发
原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/tech/java/252055.html