标签:新建 响应式编程 entity inter dep encoding 配置路由 this ota
这一篇文章介绍SpringBoot使用WebFlux响应式编程操作MongoDb数据库。
在之前一篇简单介绍了WebFlux响应式编程的操作,我们在来看一下下图,可以看到,在目前的Spring WebFlux还没有支持类似Mysql这样的关系型数据库,所以本文以MongoDb数据库为例。
接下来介绍SpringBoot使用WebFlux响应式编程操作MongoDb数据库。
新建项目,在项目中加入webflux依赖和mongodb-reactive依赖,完整pom代码如下:
<?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.dalaoyang</groupId>
<artifactId>springboot_mongodb2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_mongodb2</name>
<description>springboot_mongodb2</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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-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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件中配置mongodb数据库信息,之前文章有介绍,这里就不介绍了,配置文件代码如下:
##端口号
server.port=8888
##mongo配置
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.database=test
启动类是默认生成的,没有做任何修改,代码如下:
package com.dalaoyang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootMongodb2Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootMongodb2Application.class, args);
}
}
新建一个实体类UserInfo,代码内容没有什么特殊的,这里就不做介绍了,实体类完整代码如下:
package com.dalaoyang.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() {
}
}
新建一个UserRepository,因为是响应式编程所以这里继承ReactiveMongoRepository,代码如下:
package com.dalaoyang.repository;
import com.dalaoyang.entity.UserInfo;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
public interface UserRepository extends ReactiveMongoRepository<UserInfo,Long> {
}
新建一个UserHandler,其中分别创建了四个方法,分别是:
完整代码如下:
package com.dalaoyang.handler;
import com.dalaoyang.entity.UserInfo;
import com.dalaoyang.repository.UserRepository;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import static org.springframework.http.MediaType.APPLICATION_JSON;
@Component
public class UserHandler {
private final UserRepository repository;
public UserHandler(UserRepository repository) {
this.repository = repository;
}
//http://localhost:8888/saveUser
public Mono<ServerResponse> saveUser(ServerRequest request) {
Mono<UserInfo> user = request.bodyToMono(UserInfo.class);
return ServerResponse.ok().build(repository.insert(user).then());
}
//http://localhost:8888/deleteUser/1
public Mono<ServerResponse> deleteUser(ServerRequest request) {
Long userId = Long.valueOf(request.pathVariable("id"));
return ServerResponse.ok().build(repository.deleteById(userId).then());
}
//http://localhost:8888/user/1
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);
}
//http://localhost:8888/listUser
public Mono<ServerResponse> listUser(ServerRequest request) {
Flux<UserInfo> userList = repository.findAll();
return ServerResponse.ok().contentType(APPLICATION_JSON).body(userList, UserInfo.class);
}
}
在路由中分别配置刚刚写的4个方法路由跳转,完整代码如下:
package com.dalaoyang.router;
import com.dalaoyang.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);
}
}
到这一步完成,项目已经构建完成。
本文就不对如何安装以及启动MongoDb数据库进行介绍了,具体可以查看菜鸟教程,个人觉得这里已经介绍的很详细了,并且包含了各个系统的操作。
运行刚刚的项目,这里以postman测试为例子,在postman内Post请求保存方法(http://localhost:8888/saveUser),如图:
然后在请求查询用户方法(http://localhost:8888/user/1),如图所示,证明刚刚已经保存成功,下方body处为返回数据:
我们在多次请求新增用户方法,将Id分别修改为2,3,4,5并执行保存方法,这里就不一一截图,然后方法查询用户列表方法(http://localhost:8888/listUser),如图所示:
最后调用删除用户方法(http://localhost:8888/deleteUser/1),这里以删除ID为1的用户为例,调用后在查看所有类别如图,用户ID为1的已经被删除了:
SpringBoot使用WebFlux响应式编程操作Mongodb数据库这里已经简单整合完了,虽然和之前操作数据库有些不同,但是其实大致过程也都相似。
源码下载 :大老杨码云
标签:新建 响应式编程 entity inter dep encoding 配置路由 this ota
原文地址:https://www.cnblogs.com/dalaoyang/p/9645919.html