带有反应式 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/