实体/文档类中的@DateTimeFormat批注

时间:2019-06-20 10:29:10

标签: java spring mongodb datetime

我需要将mongo中的字符串映射到实体类中的LocalDateTime。 mongo中的日期存储为字符串,而不是ISODate。例如。 2015-02-05 10:10:10

我尝试使用@DateTimeFormat(pattern =“ yyyy-MM-dd HH:mm:ss”),但似乎未触发。

@Document
@Data
@NoArgsConstructor
public class XYZ {

    @Field(name = "datetime")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime datetime;

}

目前,XYZrepository.findAll()引发异常

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDateTime] for value '2015-02-05 10:10:10'; nested exception is java.time.format.DateTimeParseException: Text '2015-02-05 10:10:10' could not be parsed at index 10
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47) ~[spring-core-5.2.0.M3.jar:5.2.0.M3]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191) ~[spring-core-5.2.0.M3.jar:5.2.0.M3]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-5.2.0.M3.jar:5.2.0.M3]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1513) ~[spring-data-mongodb-2.2.0.RC1.jar:2.2.0.RC1]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1417) ~[spring-data-mongodb-2.2.0.RC1.jar:2.2.0.RC1]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties(MappingMongoConverter.java:392) ~[spring-data-mongodb-2.2.0.RC1.jar:2.2.0.RC1]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.populateProperties(MappingMongoConverter.java:309) ~[spring-data-mongodb-2.2.0.RC1.jar:2.2.0.RC1]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:289) ~[spring-data-mongodb-2.2.0.RC1.jar:2.2.0.RC1]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:259) ~[spring-data-mongodb-2.2.0.RC1.jar:2.2.0.RC1]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:208) ~[spring-data-mongodb-2.2.0.RC1.jar:2.2.0.RC1]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:204) ~[spring-data-mongodb-2.2.0.RC1.jar:2.2.0.RC1]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:91) ~[spring-data-mongodb-2.2.0.RC1.jar:2.2.0.RC1]
    at org.springframework.data.mongodb.core.ReactiveMongoTemplate$ReadDocumentCallback.doWith(ReactiveMongoTemplate.java:3061) ~[spring-data-mongodb-2.2.0.RC1.jar:2.2.0.RC1]
    at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:100) ~[reactor-core-3.3.0.M2.jar:3.3.0.M2]

1 个答案:

答案 0 :(得分:1)

我认为您应该实现转换器。

示例:

@WritingConverter
public class LocalDateTimeToStringConverter implements Converter<LocalDateTime, String> {

    @Override
    public String convert(LocalDateTime localDateTime) {
        return localDateTime.toString();
    }
}

@ReadingConverter
public class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
    @Override
    public LocalDateTime convert(String s) {
        return LocalDateTime.parse(s);
    }
}

@Configuration
public class MongoConfig {

    @Bean
    public MongoCustomConversions customConversions() {
        List<Converter<?, ?>> converterList = new ArrayList<>();
        converterList.add(new LocalDateTimeToStringConverter());
        converterList.add(new StringToLocalDateTimeConverter());
        return new MongoCustomConversions(converterList);
    }
}

它将把String转换为LocalDateTime并从MongoDB转换回来。