构建项目及配置

pom.xml

 <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

application.yml

spring:
application:
name: spring-boot-mongodb
data:
mongodb:
uri: mongodb://test:123456@192.168.180.113:27017/test

创建实体及具体实现

package com.topcheer.mongodb2.entity;

import org.springframework.data.annotation.Id;

public class UserInfo {

    @Id
private Long id; private String username; private String password; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public UserInfo(Long id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
} public UserInfo() {
}
}
@Component
public class UserHandler { private final UserRepository repository; public UserHandler(UserRepository repository) {
this.repository = repository;
} public Mono<ServerResponse> saveUser(ServerRequest request) {
Mono<UserInfo> user = request.bodyToMono(UserInfo.class);
return ServerResponse.ok().build(repository.insert(user).then());
} public Mono<ServerResponse> deleteUser(ServerRequest request) {
Long userId = Long.valueOf(request.pathVariable("id"));
return ServerResponse.ok().build(repository.deleteById(userId).then());
} public Mono<ServerResponse> getUser(ServerRequest request) {
Long userId = Long.valueOf(request.pathVariable("id"));
Mono<UserInfo> userInfo = repository.findById(userId);
return ServerResponse.ok().contentType(APPLICATION_JSON).body(userInfo, UserInfo.class);
} public Mono<ServerResponse> listUser(ServerRequest request) {
Flux<UserInfo> userList = repository.findAll();
return ServerResponse.ok().contentType(APPLICATION_JSON).body(userList, UserInfo.class);
} }
public interface UserRepository extends ReactiveMongoRepository<UserInfo,Long> {
}
package com.topcheer.mongodb2.router;

import com.topcheer.mongodb2.handler.UserHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse; @Configuration
public class UserRouter {
@Bean
public RouterFunction<ServerResponse> routeCity(UserHandler userHandler) {
return RouterFunctions
.route(RequestPredicates.GET("/listUser")
.and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
userHandler::listUser)
.andRoute(RequestPredicates.GET("/user/{id}")
.and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
userHandler::getUser)
.andRoute(RequestPredicates.GET("/deleteUser/{id}")
.and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
userHandler::deleteUser)
.andRoute(RequestPredicates.POST("/saveUser")
.and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
userHandler::saveUser);
}
}

测试:

最新文章

  1. Socket编程实践(1) 基本概念
  2. swift语言学习之UITableView分割线左边到头的解决
  3. Nexus私服使Maven更加强大
  4. Linux下nl命令的用法详解
  5. TortoiseSVN期望文件系统格式在“1”到“6”之间;发现格式“7”
  6. c# 高效的线程安全队列ConcurrentQueue(下) Segment类
  7. [转帖]MATLAB曲线绘制及颜色类型
  8. VC运行库版本不同导致链接.LIB静态库时发生重复定义问题的一个案例分析和总结
  9. SQL查询语句联系
  10. 初识QT
  11. dos4章
  12. android app demo
  13. python全栈开发 * 24 知识点汇总 * 180705
  14. Python 实现累加计数的几种方法
  15. 008_MAC 终端使用技巧
  16. MongoDB 数据管理
  17. python webdriver api-上传文件的三种方法
  18. GridControl详解(二)表格的列名配置
  19. springmvc pojo
  20. 怎样优雅的研究 RGSS3 番外(一) ruby 实现的后缀自己主动机

热门文章

  1. 什么是Shell?Shell脚本基础知识详细介绍
  2. Docker开启ssh服务
  3. MySQL-快速入门(7)索引
  4. 字符串中的replace方法
  5. Eureka 源码分析之 Eureka Server
  6. js获取url中的参数(解决中文乱码)
  7. 搜索专题: HDU1501Zipper
  8. 通过编写串口助手工具学习MFC过程——(七)添加Tab Control控件
  9. nginx启动报错
  10. vue项目1-pizza点餐系统9-axios实现数据存储