Skip to content
Advertisement

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

Does WebFlux Spring Boot with reactive MongoDB supports Auditing? I tried to use @CreatedDate and it did not work for me. 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

Do I miss something or Auditing does not work with reactive MongoDB?

Advertisement

Answer

I resolved the issue by using @EnableReactiveMongoAuditing, not @EnableMongoAuditing as I initially did. Apparently the reactive annotation should be used with 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:

{ "_id" : ObjectId("628a01d77f74d46c62a5bb36"), "text" : "Hello", "createdDate" : ISODate("2022-05-22T09:26:47.280Z"), "_class" : "com.mgtest.app.model.dao.Message" }
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement