@EnableMongoAuditing and @CreatedDate Auditing not working in Spring Boot 2.4.3

Viewed 2031

I'm following this example https://github.com/hantsy/spring-reactive-sample/blob/master/boot-exception-handler/src/main/java/com/example/demo/DemoApplication.java ...which works -- sets the createDate MongoDB field on creation. The version there is 2.1.6.RELEASE. However, when I upgrade this to 2.4.2, createDate is no longer set. There are no warnings, it seems just to have stopped working.

The model class is:

@Document
@Data
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
class Post {

    @Id
    private String id;
    private String title;
    private String content;

    @CreatedDate
    private LocalDateTime createdDate;
    @Version
    private Integer version;
}

Also, MongoDB auditing is set:

@SpringBootApplication
@EnableMongoAuditing
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

Is this is a known issue by any chance? I couldn't find any migration notes on this. How to enable auditing in the latest Spring Boot version?

Update:

Looks like the latest version where it worked is with spring-boot-starter-parent 2.3.7.RELEASE. Stops working when switch this to 2.4.0.

1 Answers

Resolved by using the new @EnableReactiveMongoAuditing annotation, plus a bean like

@Bean
public ReactiveAuditorAware<String> auditorProvider() {
    return () -> Mono.just("Me");
}

The issue was likely related to the fact I had configured the app using @EnableReactiveMongoRepositories

Related