审计(@CreatedDate)不适用于WebFlux Spring Boot with reactive MongoD



WebFlux Spring Boot with reactive MongoDB支持审计吗?我试着使用@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

我错过了什么或审计不与响应式MongoDB工作吗?

我通过使用@EnableReactiveMongoAuditing解决了这个问题,而不是像我最初做的那样使用@EnableMongoAuditing。显然,响应式注释应该与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" }

相关内容

  • 没有找到相关文章

最新更新