Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

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

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.

question from:https://stackoverflow.com/questions/65854050/enablemongoauditing-and-createddate-auditing-not-working-in-spring-boot-2-4-3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...