无法通过 repo 删除文档--> 使用 Webflux 在 Spring Boot 2.x 中删除
Not able to delete a document by repo--> delete in Spring Boot 2.x with Webflux
我正在使用 Spring Boot 2.0.4.RELEASE、webflux 和 mongodb-reactive,我的所有操作都正常 POST、GET、PUT,但是删除不管用。
可以在 github.com Source code link
此处查看演示应用程序的完整源代码
这是我的文档class:
@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;
}
}
这是我的 DELETE 处理程序方法 request.method 成功返回 OK(200) 响应但是当我看到 mongdb 时,记录没有被删除。
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 link。
您的 delete
未应用,因为它不是反应链的一部分 - 请参阅 this item in the reactor documentation。如果操作未链接,则不会订阅您的代码的那部分,也不会执行它。
在这种特殊情况下,您必须确保在删除操作完成后返回 OK 响应:
return employeeRepository.delete(employee)
.then(ServerResponse.ok().build());
注意:您的员工存储库有点奇怪,因为它使用 UUID
作为文档的 ID,而您似乎在所有其他地方都使用 String
。也许您应该在存储库界面上使用 String
。
我正在使用 Spring Boot 2.0.4.RELEASE、webflux 和 mongodb-reactive,我的所有操作都正常 POST、GET、PUT,但是删除不管用。 可以在 github.com Source code link
此处查看演示应用程序的完整源代码这是我的文档class:
@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;
}
}
这是我的 DELETE 处理程序方法 request.method 成功返回 OK(200) 响应但是当我看到 mongdb 时,记录没有被删除。
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 link。
您的 delete
未应用,因为它不是反应链的一部分 - 请参阅 this item in the reactor documentation。如果操作未链接,则不会订阅您的代码的那部分,也不会执行它。
在这种特殊情况下,您必须确保在删除操作完成后返回 OK 响应:
return employeeRepository.delete(employee)
.then(ServerResponse.ok().build());
注意:您的员工存储库有点奇怪,因为它使用 UUID
作为文档的 ID,而您似乎在所有其他地方都使用 String
。也许您应该在存储库界面上使用 String
。