尝试将JodaTime的LocalDate
与Spring Data MongoDB和Spring 4一起使用,但我得到以下异常:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.model.MappingException: No property node found on entity class com.th.model.Price to bind constructor parameter to!] with root cause
org.springframework.data.mapping.model.MappingException: No property node found on entity class com.th.model.Price 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:249)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:230)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:190)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:186)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:77)
at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:2121)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1805)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1628)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1611)
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:535)
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:526)
at com.th.model.Company.getPrices(Company.java:255)
at com.th.controller.Home.getPrices(Home.java:32)
…
这是我的配置:
<mongo:db-factory id="mongoDbFactory" dbname="th" host="localhost"/>
<mongo:mapping-converter id="mappingConverter">
<mongo:custom-converters>
<mongo:converter>
<bean class="com.th.tool.MongoDBConverters.DateToLocalDateConverter"/>
</mongo:converter>
<mongo:converter>
<bean class="com.th.tool.MongoDBConverters.LocalDateToDateConverter"/>
</mongo:converter>
</mongo:custom-converters>
</mongo:mapping-converter>
<bean class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg name="mongoConverter" ref="mappingConverter"/>
</bean>
public class MongoDBConverters {
public static class DateToLocalDateConverter implements Converter<Date, LocalDate> {
@Override
public LocalDate convert(Date source) {
return source == null ? null : LocalDate.fromDateFields(source);
}
}
public static class LocalDateToDateConverter implements Converter<LocalDate, Date> {
@Override
public Date convert(LocalDate source) {
return source == null ? null : source.toDate();
}
}
}
答案 0 :(得分:6)
显然我不能有这样的构造函数:
public Price(String companyId, JsonNode node) {
this.companyId = companyId;
if(node != null) {
this.date = Tools.getLocalDate(node.get("Date").getTextValue());
this.closePrice = Double.parseDouble(node.get("Close").getTextValue());
this.volume = Double.parseDouble(node.get("Volume").getTextValue());
}
}
具有直接分配工作的正常工作:
public Price(String companyId, LocalDate date, double closePrice, double volume) {
this.companyId = companyId;
this.date = date;
this.closePrice = closePrice;
this.volume = volume;
}
答案 1 :(得分:4)
在构造对象之前,您需要在封闭类static
的实例可用时创建内部类MongoDBConverters
。制作此课程static
并不需要。{/ p>
public class MongoDBConverters {
public static class DateToLocalDateConverter implements Converter<Date, LocalDate> {
@Override
public LocalDate convert(Date source) {
return source == null ? null : LocalDate.fromDateFields(source);
}
}
public static class LocalDateToDateConverter implements Converter<LocalDate, Date> {
@Override
public Date convert(LocalDate source) {
return source == null ? null : source.toDate();
}
}
}
有关详细信息,请查看Java inner class and static nested class。