<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wnx.mall.tiny</groupId> <artifactId>spring-boot-mongodb-crud</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-mongodb-crud</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
spring: data: mongodb: host: localhost port: 27017 database: webflux
package com.wnx.mall.tiny.entity; import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "user") @Data public class User { @Id private String id; private String name; private Integer age; private String description; }
package com.wnx.mall.tiny; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories; @SpringBootApplication @EnableReactiveMongoRepositories public class SpringBootMongodbCrudApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMongodbCrudApplication.class, args); } }
package com.wnx.mall.tiny.repository; import com.wnx.mall.tiny.entity.User; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.repository.ReactiveMongoRepository; import org.springframework.stereotype.Repository; import reactor.core.publisher.Flux; @Repository public interface UserRepository extends ReactiveMongoRepository<User, String> { }
package com.wnx.mall.tiny.service; import com.wnx.mall.tiny.entity.User; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; public interface UserService { /** * 创建用户 * @param user * @return */ Mono<User> create(User user); /** * 根据ID删除用户 * @param id * @return */ Mono<Void> delete(String id); /** * 根据ID更新用户 * @param id * @param user * @return */ Mono<User> update(String id,User user); /** * 查询全部用户 * @return */ Flux<User> findAll(); /** * 根据ID查询用户 * @param id * @return */ Mono<User> findById(String id); }
package com.wnx.mall.tiny.service.impl; import com.wnx.mall.tiny.entity.User; import com.wnx.mall.tiny.repository.UserRepository; import com.wnx.mall.tiny.service.UserService; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; import org.springframework.stereotype.Service; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import javax.annotation.Resource; @Service public class UserServiceServiceImpl implements UserService { @Resource private UserRepository userRepository; @Resource private ReactiveMongoTemplate template; @Override public Mono<User> create(User user) { return userRepository.insert(user); } @Override public Mono<Void> delete(String id) { return userRepository.deleteById(id); } @Override public Mono<User> update(String id, User user) { return userRepository.findById(id).flatMap(x -> { x.setName(user.getName()); x.setAge(user.getAge()); x.setDescription(user.getDescription()); return userRepository.save(x); }); } @Override public Flux<User> findAll() { return userRepository.findAll(); } @Override public Mono<User> findById(String id) { return userRepository.findById(id); } }
package com.wnx.mall.tiny.controller; import com.wnx.mall.tiny.entity.User; import com.wnx.mall.tiny.service.UserService; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import javax.annotation.Resource; @RestController @RequestMapping("user") public class UserController { @Resource private UserService userService; @PostMapping("/save") public Mono<User> create(User user){ return userService.create(user); } /** * 删除返回200,删除失败返回404 * @param id * @return */ @GetMapping("/delete/{id}") public Mono<ResponseEntity<Void>>delete(@PathVariable(name = "id") String id){ return userService.delete(id). then(Mono.just(new ResponseEntity<Void>(HttpStatus.OK))) .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND)); } /** * 存在返回修改的User * 不存在返回404 * @param id * @param user * @return */ @PostMapping("/update/{id}") public Mono<ResponseEntity<User>> update(@PathVariable(name = "id")String id,User user){ return userService.update(id, user) .map(u -> new ResponseEntity<>(u, HttpStatus.OK)) .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND)); } /** * 以数组的形式一次性返回全部数据 * @return */ @GetMapping("/findAll") public Flux<User> findAll(){ return userService.findAll(); } /** * 以Server sent events 的形式返回 * @return */ @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux<User> getUsersStream() { return userService.findAll(); } /** * 根据用户 id查找 * 存在返回,不存在返回 404 */ @GetMapping("/findById/{id}") public Mono<ResponseEntity<User>> getUser(@PathVariable String id) { return userService.findById(id) .map(user -> new ResponseEntity<>(user, HttpStatus.OK)) .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND)); } }
增删改,就不操作了。
http://localhost:8080/user/stream data:{"id":"1","name":"王乃醒","age":24,"description":"这是王乃兴WebFlux测试"} data:{"id":"2","name":"王乃醒2","age":24,"description":"这是王乃兴WebFlux测试"} data:{"id":"3","name":"王乃醒3","age":24,"description":"这是王乃兴WebFlux测试"} data:{"id":"4","name":"王乃醒4","age":24,"description":"这是王乃兴WebFlux测试"} data:{"id":"5","name":"王乃醒5","age":24,"description":"这是王乃兴WebFlux测试"} data:{"id":"6","name":"王乃醒6","age":24,"description":"这是王乃兴WebFlux测试"} data:{"id":"7","name":"王乃醒7","age":24,"description":"这是王乃兴WebFlux测试"} data:{"id":"8","name":"王乃醒8","age":24,"description":"这是王乃兴WebFlux测试"} data:{"id":"9","name":"王乃醒9","age":24,"description":"这是王乃兴WebFlux测试"} data:{"id":"10","name":"王乃醒10","age":24,"description":"这是王乃兴WebFlux测试"} http://localhost:8080/user/findAll [ { "id": "1", "name": "王乃醒", "age": 24, "description": "这是王乃兴WebFlux测试" }, { "id": "2", "name": "王乃醒2", "age": 24, "description": "这是王乃兴WebFlux测试" }, { "id": "3", "name": "王乃醒3", "age": 24, "description": "这是王乃兴WebFlux测试" }, { "id": "4", "name": "王乃醒4", "age": 24, "description": "这是王乃兴WebFlux测试" }, { "id": "5", "name": "王乃醒5", "age": 24, "description": "这是王乃兴WebFlux测试" }, { "id": "6", "name": "王乃醒6", "age": 24, "description": "这是王乃兴WebFlux测试" }, { "id": "7", "name": "王乃醒7", "age": 24, "description": "这是王乃兴WebFlux测试" }, { "id": "8", "name": "王乃醒8", "age": 24, "description": "这是王乃兴WebFlux测试" }, { "id": "9", "name": "王乃醒9", "age": 24, "description": "这是王乃兴WebFlux测试" }, { "id": "10", "name": "王乃醒10", "age": 24, "description": "这是王乃兴WebFlux测试" } ] http://localhost:8080/user/findById/2 { "id": "2", "name": "王乃醒2", "age": 24, "description": "这是王乃兴WebFlux测试" }