@ Путь Максима мне тоже помог. Мне пришлось немного его настроить для Spring Framework 5 и Spring Boot 2:
@Bean
@Primary
public MappingMongoConverter mongoConverter(
@Autowired MongoMappingContext mongoMappingContext,
@Autowired MongoDbFactory mainMongoFactory,
@Autowired MongoCustomConversions conversions
) {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mainMongoFactory);
MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
mongoConverter.setMapKeyDotReplacement("#");
mongoConverter.afterPropertiesSet();
mongoConverter.setCustomConversions(conversions);
return mongoConverter;
}
@Bean
public MongoMappingContext mongoMappingContext() {
MongoMappingContext context = new MongoMappingContext();
context.setSimpleTypeHolder(new SimpleTypeHolder(new HashSet<>(Arrays.asList(
DateTime.class,
LocalDateTime.class
)), MongoSimpleTypes.HOLDER));
return context;
}
@Bean
public MongoCustomConversions customConversions() {
List<Converter<?, ?>> converterList = new ArrayList<Converter<?, ?>>();
converterList.add(new MongoLocalDateTimeFromStringConverter());
converterList.add(new MongoDateTimeFromStringConverter());
return new MongoCustomConversions(converterList);
}
private static final class MongoLocalDateTimeFromStringConverter implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String source) {
return source == null ? null : LocalDateTime.parse(source);
}
}
private static final class MongoDateTimeFromStringConverter implements Converter<String, DateTime> {
@Override
public DateTime convert(String source) {
return source == null ? null : DateTime.parse(source);
}
}