问题描述
我尝试使用Java 8 Date Time API的LocalTime与Spring数据MongoDB。插入文档按预期工作,但是当我尝试阅读文档时,我收到以下错误:
线程中的异常 mainjava.lang.IllegalStateException:在org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:637)中执行CommandLineRunner
....
导致:org。 springframework.data.mapping.model.MappingException:在实体类java.time.LocalDate上找不到属性null将构造函数参数绑定到!
在org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:74)
在org.springframework.data.mapping.model.SpELExpressionParameterValueProvider.getParameterValue(SpELExpressionParameterValueProvider.java:63)
在org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:71)
在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:257)
在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:237)
在org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter。 java:1109)
在org.springframework.data.mongodb.core.convert.MappingMongoConverter.access $ 100(MappingMongoConverter.java:78)
在org.springframework.data.mongodb.core.convert.MappingMongoConverter $ MongoD bPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1058)
在org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:789)
在org.springframework.data.mongodb。 core.convert.MappingMongoConverter $ 1.doWithPersistentProperty(MappingMongoConverter.java:270)
在org.springframework.data.mongodb.core.convert.MappingMongoConverter $ 1.doWithPersistentProperty(MappingMongoConverter.java:263)
在org。 springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:261)
在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:263)
在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:237)
在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:201)
在org.springframework.data.mongodb.co re.convert.MappingMongoConverter.read(MappingMongoConverter.java:197)
在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:78)
在org.springframework。 data.mongodb.core.MongoTemplate $ ReadDbObjectCallback.doWith(MongoTemplate.java:2005)
在org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1699)
在org。 springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1522)
在org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1506)
在org。 springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:532)
在org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:217)
在org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:174)
在org.springframework.data.mongodb.r epository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
在java.lang.reflect.Method.invoke(Method.java:483)
在org.springframework.data。 repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:358)
在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:343)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
在com。 sun.proxy。$ Proxy28.findAll(Unknown Sou rce)
在hello.Application.run(Application.java:36)
在org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:634)
... 5更多
我从Spring网站的例子中尝试过:
我刚刚更改为客户端有一个出生日期:
package hello;
import org.springframework.data.annotation.Id;
import java.time.LocalDate;
public class Customer {
@Id
private String id;
private String firstName;
private String lastName;
私人LocalDate birthDay;
public Customer(){}
public Customer(String firstName,String lastName,LocalDate birthDay){
this.firstName = firstName;
this.lastName = lastName;
this.birthDay = birthDay;
}
@Override
public String toString(){
return String.format(
Customer [id =%s,firstName ='% s',lastName ='%s'],
id,firstName,lastName);
}
}
我为所有4个转换选项写了一小段代码:
- DateToLocalDateTimeConverter
- DateToLocalDateConverter
- LocalDateTimeToDateConverter
- LocalDateToDateConverter
这是一个例子
public class DateToLocalDateTimeConverter implements Converter< Date,LocalDateTime> {
@Override
public LocalDateTime convert(Date source){
return source == null? null:LocalDateTime.ofInstant(source.toInstant(),ZoneId.systemDefault());
}
}
然后将其包含在mongodb的xml配置中连接我能够在java 8日期与mongodb工作(记得添加所有转换器):
< mongo:mapping变压器>
< mongo:custom-converters>
< mongo:converter>
< bean class =package.DateToLocalDateTimeConverter/>
< / mongo:converter>
< / mongo:custom-converters>
< / mongo:mapping-converter>
I try to use the LocalTime from Java 8 Date Time API with Spring Data MongoDB. Inserting the document works as expected, but when I try to read the document, I get the following error:
Exception in thread "main" java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:637)
....
Caused by: org.springframework.data.mapping.model.MappingException: No property null found on entity class java.time.LocalDate to bind constructor parameter to!
at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:74)
at org.springframework.data.mapping.model.SpELExpressionParameterValueProvider.getParameterValue(SpELExpressionParameterValueProvider.java:63)
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:71)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:257)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:237)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1109)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$100(MappingMongoConverter.java:78)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1058)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:789)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:270)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:263)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:261)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:263)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:237)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:201)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:197)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:78)
at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:2005)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1699)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1522)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1506)
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:532)
at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:217)
at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:174)
at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:358)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:343)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy28.findAll(Unknown Source)
at hello.Application.run(Application.java:36)
at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:634)
... 5 more
I tried this with the example from the Spring website: http://spring.io/guides/gs/accessing-data-mongodb/I just changed to Customer to have a birthdate:
package hello;
import org.springframework.data.annotation.Id;
import java.time.LocalDate;
public class Customer {
@Id
private String id;
private String firstName;
private String lastName;
private LocalDate birthDay;
public Customer() {}
public Customer(String firstName, String lastName, LocalDate birthDay) {
this.firstName = firstName;
this.lastName = lastName;
this.birthDay = birthDay;
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}
I wrote this little bit of code for all 4 of these conversion options:
- DateToLocalDateTimeConverter
- DateToLocalDateConverter
- LocalDateTimeToDateConverter
- LocalDateToDateConverter
Here is an example
public class DateToLocalDateTimeConverter implements Converter<Date, LocalDateTime> {
@Override
public LocalDateTime convert(Date source) {
return source == null ? null : LocalDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
}
}
Then by including this in the xml configuration for the mongodb connection I was able to work in java 8 dates with mongodb (remember to add all the converters):
<mongo:mapping-converter>
<mongo:custom-converters>
<mongo:converter>
<bean class="package.DateToLocalDateTimeConverter" />
</mongo:converter>
</mongo:custom-converters>
</mongo:mapping-converter>
这篇关于Spring数据MongoDB与Java 8 LocalDate MappingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!