我正在使用HATEOS和spring数据mongo! 尝试获取数据时出错 -
2017-01-23 13:31:43.859 INFO 13108 --- [nio-8080-exec-1] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:9179}] to hkl101632.hk.hsbc:5255
2017-01-23 13:31:44.672 ERROR 13108 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate javax.xml.datatype.XMLGregorianCalendar using constructor public javax.xml.datatype.XMLGregorianCalendar() with arguments ] with root cause
java.lang.InstantiationException: null
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) ~[na:1.8.0_66]
at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_66]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:76) ~[spring-data-commons-1.12.6.RELEASE.jar:na]
at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:83) ~[spring-data-commons-1.12.6.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:252) ~[spring-data-mongodb-1.9.6.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:232) ~[spring-data-mongodb-1.9.6.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1197) ~[spring-data-mongodb-1.9.6.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$200(MappingMongoConverter.java:79) ~[spring-data-mongodb-1.9.6.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1145) ~[spring-data-mongodb-1.9.6.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:871) ~[spring-data-mongodb-1.9.6.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:284) ~[spring-data-mongodb-1.9.6.RELEASE.jar:na]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:272) ~[spring-data-mongodb-1.9.6.RELEASE.jar:na]
我能够使用xml将数据推送到pojo映射,但在重新启动时,它无法从mongo中提取数据,因为它尝试转换为XMLGregorianCalendar实例!
我们是否可以在不改变现有xml-pojo映射的情况下编写根级转换器,以使用java.util.Date转换和保存datetime实例。
以下是我的示例代码!
保存时输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Entity>
<Header>
<MsgId>12345wqw</MsgId>
<DateTimeField>2009-09-28T14:07:00</DateTimeField>
<Info>
<Name>Some Name</Name>
<Address>
<StrtNm>Times Square</StrtNm>
<BldgNb>7</BldgNb>
<PstCd>NY 10036</PstCd>
<TwnNm>New York</TwnNm>
<Ctry>US</Ctry>
</Address>
</Info>
</Header>
</Entity>
</Document>
Mapped Pojo:
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.datatype.XMLGregorianCalendar;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Header", propOrder = {"msgId", "DateTimeField", "Info"})
public class GroupHeader48 {
@XmlElement(name = "MsgId", required = true)
protected String msgId;
@XmlElement(name = "DateTimeField", required = true)
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar creDtTm;
@XmlElement(name = "Info", required = true)
protected Information info;
public String getMsgId() {
return this.msgId;
}
public void setMsgId(final String value) {
this.msgId = value;
}
public XMLGregorianCalendar getCreDtTm() {
return this.creDtTm;
}
public void setCreDtTm(final XMLGregorianCalendar value) {
this.creDtTm = value;
}
public Information getInfo() {
return this.info;
}
public void setInitgPty(final Information value) {
this.info = value;
}
}
我也尝试过使用适配器,但是我有很多pojos,其中使用了datetime字段,并且无法控制更改映射,因为它来自另一个进程