尝试在MongoDB中使用新的java.time.OffsetDateTime
对象。我正在使用org.springframework.data:spring-data-mongodb:1.8.2.RELEASE
。它似乎能够很好地写入数据库,但是当我尝试读取数据时,它会抛出以下异常。如果我改为将对象更改为LocalDateTime
,则可以成功读取/写入数据库。是否需要执行其他配置才能添加对OffsetDateTime对象的支持?
org.springframework.data.mapping.model.MappingException: No property null found on entity class java.time.OffsetDateTime to bind constructor parameter to!
at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:74) ~[spring-data-commons-1.11.2.RELEASE.jar!/:na]
at org.springframework.data.mapping.model.SpELExpressionParameterValueProvider.getParameterValue(SpELExpressionParameterValueProvider.java:63) ~[spring-data-commons-1.11.2.RELEASE.jar!/:na]
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:71) ~[spring-data-commons-1.11.2.RELEASE.jar!/:na]
at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:83) ~[spring-data-commons-1.11.2.RELEASE.jar!/:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:251) ~[spring-data-mongodb-1.8.2.RELEASE.jar!/:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:231) ~[spring-data-mongodb-1.8.2.RELEASE.jar!/:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1186) ~[spring-data-mongodb-1.8.2.RELEASE.jar!/:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$200(MappingMongoConverter.java:78) ~[spring-data-mongodb-1.8.2.RELEASE.jar!/:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1134) ~[spring-data-mongodb-1.8.2.RELEASE.jar!/:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:870) ~[spring-data-mongodb-1.8.2.RELEASE.jar!/:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:283) ~[spring-data-mongodb-1.8.2.RELEASE.jar!/:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:271) ~[spring-data-mongodb-1.8.2.RELEASE.jar!/:na]
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:309) ~[spring-data-commons-1.11.2.RELEASE.jar!/:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:271) ~[spring-data-mongodb-1.8.2.RELEASE.jar!/:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:231) ~[spring-data-mongodb-1.8.2.RELEASE.jar!/:na]
答案 0 :(得分:2)
所以看来当前的JSR-310支持仅适用于非时间分区的日期对象。这可以解释为什么OffsetDateTime无效。
https://spring.io/blog/2015/03/26/what-s-new-in-spring-data-fowler
此设置将确保您的应用程序包和 将扫描用于JSR-310转换器的Spring Data JPA 交给持久性提供者。找一个完整的例子 在我们的Spring Data Examples存储库中。请注意,由于事实 转换器只是将JSR-310类型转换为旧版Date 实例,只有非时间分区(例如LocalDateTime等) 支撑。
我能够通过回退到使用Joda DateTime对象来解决这个问题。我必须做以下事情:
compile('com.fasterxml.jackson.datatype:jackson-datatype-joda:2.6.3')
添加到gradle.build文件。spring.jackson.serialization.write_dates_as_timestamps=false
添加到application.properties文件中,使输出具有人类可读性。import org.joda.time.DateTime;
并使用此代替OffsetDateTime