java - 审核 (@CreatedDate) 不适用于具有反应式 MongoDB 的 WebFlux Spring Boot

标签 java mongodb reactive audit spring-data-r2dbc

带有反应式 MongoDB 的 WebFlux Spring Boot 支持审核吗? 我尝试使用 @CreatedDate 但它对我不起作用。 这是我的配置:

@Configuration
@EnableReactiveMongoRepositories
@EnableMongoAuditing
@AllArgsConstructor
public class ReactiveMongoConfiguration extends AbstractReactiveMongoConfiguration {
    ...
}

这是我的文档类

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mongodb.core.mapping.Document;
...    
import java.util.Date;

@Document
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Message implements Persistable<String> {
  @Id private String id;

  private String text;

  @CreatedDate
  private Date createdDate;

  @Override
  public boolean isNew() {
    return createdDate == null;
  }

这里是消息存储库

@Repository
public interface IMessageRepository extends ReactiveMongoRepository<Message, String> {}

当我保存消息messageRepository.save(message)时,我总是有createdDate=null

我是否遗漏了什么或者审核不适用于响应式(Reactive) MongoDB?

最佳答案

我通过使用 @EnableReactiveMongoAuditing 解决了该问题,而不是像我最初那样使用 @EnableMongoAuditing。显然,响应式(Reactive)注释应该与ReactiveMongoRepositories一起使用。所以正确的配置如下:

@Configuration
@EnableReactiveMongoRepositories
@EnableReactiveMongoAuditing
@AllArgsConstructor
public class ReactiveMongoConfiguration extends AbstractReactiveMongoConfiguration {
...
} 

因此,保存消息后,会自动添加相应的 createdDate:

{ "_id" : ObjectId("628a01d77f74d46c62a5bb36"), "text" : "Hello", "createdDate" : ISODate("2022-05-22T09:26:47.280Z"), "_class" : "com.mgtest.app.model.dao.Message" }

关于java - 审核 (@CreatedDate) 不适用于具有反应式 MongoDB 的 WebFlux Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72330068/

相关文章:

java - 开发与同一数据库交互的 Android 应用程序和 Java 应用程序

Java - 构造函数是静态的吗?

java - 使用 Spring Webflux 将 Flux 转换为树

java - Spring Reactive WebClient

Java摩尔斯电码翻译器

java - 有没有办法在运行时加载类 jar 和包?

javascript - 为什么我的 Mongoose 数组没有被填充,甚至没有存储值?

java - 如何在 Spring Data MongoDB 中保存和查询动态字段?

bash - 检查数据库是否存在

java - Spring WebClient - HTTP 错误状态的自定义响应回调