[英]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.