简体   繁体   English

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

[英]Auditing (@CreatedDate) does not work for WebFlux Spring Boot with reactive MongoDB

Does WebFlux Spring Boot with reactive MongoDB supports Auditing?带有反应式 MongoDB 的 WebFlux Spring Boot 是否支持审计? I tried to use @CreatedDate and it did not work for me.我尝试使用@CreatedDate ,但它对我不起作用。 Here is my configuration:这是我的配置:

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

Here is my document class这是我的文档类

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;
  }

Here is Message repository这是消息存储库

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

When I save message messageRepository.save(message) I always have createdDate=null当我保存消息messageRepository.save(message)我总是createdDate=null

Do I miss something or Auditing does not work with reactive MongoDB?我错过了什么或审计不适用于反应式 MongoDB?

I resolved the issue by using @EnableReactiveMongoAuditing , not @EnableMongoAuditing as I initially did.我通过使用@EnableReactiveMongoAuditing解决了这个问题,而不是像我最初那样使用@EnableMongoAuditing Apparently the reactive annotation should be used with ReactiveMongoRepositories .显然反应式注释应该与ReactiveMongoRepositories一起使用。 So the correct configuration is the following:所以正确的配置如下:

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

So after saving a message the corresponding createdDate is added automatically:因此,在保存消息后,会自动添加相应的createdDate

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mongodb 在 spring boot 中审计以保存 createdDate、lastModifiedDate、createdBy、lastModifiedBy - mongodb auditing in spring boot for saving createdDate, lastModifiedDate, createdBy, lastModifiedBy WebFlux Spring Boot @Transactional 与反应式 MongoDB - WebFlux Spring Boot @Transactional with reactive MongoDB Spring审核-Spring如何自动将@CreatedDate和@LastModifiedDate插入数据库? - Spring Auditing - How does spring insert @CreatedDate and @LastModifiedDate into database automaticaly? Spring Boot Webflux 反应式 API - Spring Boot Webflux reactive api 使用 MySql 启动响应式 Spring (Webflux) - Spring boot reactive (Webflux) with MySql Spring boot mongodb审计报错 - Spring boot mongodb auditing error 在Maven中看不到spring-boot-starter-data-mongodb-active和spring-boot-starter-webflux依赖性 - Dont see spring-boot-starter-data-mongodb-reactive and spring-boot-starter-webflux dependency in maven 更新 1 个或多个特定字段 MongoDB 使用 Spring 启动 WebFlux,Spring 数据 Z206E3718AF0921CC1D12F80ZReposit7 - Update 1 or multiple specific field MongoDB using Spring boot WebFlux,Spring Data MongoDB Reactive and ReactiveMongoRepository Spring Mongo 审计不工作@CreatedDate @CreatedBy - Spring Mongo Auditing not working @CreatedDate @CreatedBy Spring Data MongoDB - 使用自定义 Id 字段时,注释 @CreatedDate 不起作用 - Spring Data MongoDB - Annotation @CreatedDate does not work while using with custom Id field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM