代码之家  ›  专栏  ›  技术社区  ›  kj007 XdebugX

无法使用Webflux在Spring Boot 2.x中通过repo-->delete删除文档

  •  1
  • kj007 XdebugX  · 技术社区  · 6 年前

    完整的源代码演示应用程序可以在这里看到github.com网站 Source code link

    这是我的文档类:

    @Document(collection = "users")
    public class Employee implements Serializable {
    
        @Id
        private String id = UUID.randomUUID().toString();
    
        private String firstName;
    
        private String lastName;
    
        private String email;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    }
    

     public Mono<ServerResponse> deleteAnEmployee(ServerRequest request) {
            String employeeId = request.pathVariable("id");
            return employeeRepository.findById(employeeId)
                    .flatMap(employee -> {
                        employeeRepository.delete(employee);
                        return ServerResponse.ok().build();
                    }).switchIfEmpty(ServerResponse.notFound().build());
        }
    

    我是不是漏了什么suggest.pom路由器、repo等请参阅描述中提供的gihub链接。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Brian Clozel    6 年前

    delete 不应用,因为它不是反应链的一部分-请参阅 this item in the reactor documentation . 如果操作没有链接,那么就不会订阅代码的这一部分,也不会执行它。

                    return employeeRepository.delete(employee)
                               .then(ServerResponse.ok().build());
    

    注意:您的员工存储库有点奇怪,因为它正在使用 UUID 作为文档的id,而您似乎正在使用 String 字符串 而是在存储库界面上。