Moxy-JAXB及其接口

Moxy-JAXB及其接口,jaxb,moxy,Jaxb,Moxy,关于如何使用Moxy和JaxB接口,已经有相当多的讨论 见例 布莱斯·道格汉的回答部分被接受,部分被拒绝。 我试着把答案改写成英文 见 测试用例。目前有一个 @Ignore 添加到测试用例中。如果取消对@Ignore的注释,则根据尝试的解组方法,会出现不同的错误: 1。使用xml元描述时,错误为: javax.xml.bind.UnmarshalException - with linked exception: [Exception [EclipseLink-25008] (E

关于如何使用Moxy和JaxB接口,已经有相当多的讨论

见例

布莱斯·道格汉的回答部分被接受,部分被拒绝。 我试着把答案改写成英文

测试用例。目前有一个

@Ignore
添加到测试用例中。如果取消对@Ignore的注释,则根据尝试的解组方法,会出现不同的错误:

1。使用xml元描述时,错误为:

javax.xml.bind.UnmarshalException
 - with linked exception:
[Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor with default root element UserManager was not found in the project]
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:1110)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:638)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:214)
    at com.bitplan.jaxb.TestJaxbFactory.unmarshalFromContext(TestJaxbFactory.java:166)
    at com.bitplan.jaxb.TestJaxbFactory.testUserManager(TestJaxbFactory.java:209)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at 
javax.xml.bind.UnmarshalException
 - with linked exception:
[Exception [EclipseLink-44] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class indicator field from database row [org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl@38c6f217].
Descriptor: XMLDescriptor(com.bitplan.rest.users.User --> [])]
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:225)
    at com.bitplan.jaxb.TestJaxbFactory.unmarshalFromContext(TestJaxbFactory.java:166)
    at com.bitplan.jaxb.TestJaxbFactory.testUserManager(TestJaxbFactory.java:215)
java.lang.ClassCastException: java.lang.String cannot be cast to com.bitplan.rest.users.User
    at com.bitplan.jaxb.TestJaxbFactory.testUserManager(TestJaxbFactory.java:219)
2。使用ObjectFactory方法,我得到:

javax.xml.bind.UnmarshalException
 - with linked exception:
[Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor with default root element UserManager was not found in the project]
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:1110)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:638)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:214)
    at com.bitplan.jaxb.TestJaxbFactory.unmarshalFromContext(TestJaxbFactory.java:166)
    at com.bitplan.jaxb.TestJaxbFactory.testUserManager(TestJaxbFactory.java:209)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at 
javax.xml.bind.UnmarshalException
 - with linked exception:
[Exception [EclipseLink-44] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class indicator field from database row [org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl@38c6f217].
Descriptor: XMLDescriptor(com.bitplan.rest.users.User --> [])]
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:225)
    at com.bitplan.jaxb.TestJaxbFactory.unmarshalFromContext(TestJaxbFactory.java:166)
    at com.bitplan.jaxb.TestJaxbFactory.testUserManager(TestJaxbFactory.java:215)
java.lang.ClassCastException: java.lang.String cannot be cast to com.bitplan.rest.users.User
    at com.bitplan.jaxb.TestJaxbFactory.testUserManager(TestJaxbFactory.java:219)
3。使用适用于我得到的客户/订单的JaxbFactory方法:

javax.xml.bind.UnmarshalException
 - with linked exception:
[Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor with default root element UserManager was not found in the project]
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:1110)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:638)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:214)
    at com.bitplan.jaxb.TestJaxbFactory.unmarshalFromContext(TestJaxbFactory.java:166)
    at com.bitplan.jaxb.TestJaxbFactory.testUserManager(TestJaxbFactory.java:209)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at 
javax.xml.bind.UnmarshalException
 - with linked exception:
[Exception [EclipseLink-44] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class indicator field from database row [org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl@38c6f217].
Descriptor: XMLDescriptor(com.bitplan.rest.users.User --> [])]
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:225)
    at com.bitplan.jaxb.TestJaxbFactory.unmarshalFromContext(TestJaxbFactory.java:166)
    at com.bitplan.jaxb.TestJaxbFactory.testUserManager(TestJaxbFactory.java:215)
java.lang.ClassCastException: java.lang.String cannot be cast to com.bitplan.rest.users.User
    at com.bitplan.jaxb.TestJaxbFactory.testUserManager(TestJaxbFactory.java:219)
我该如何让它工作?

这三种方法中哪一种是正常的,而Moxy只是有问题,或者方法中有错误?

Testcode

@测试
@忽略
public void testUnMarshalViaMetaXML()引发异常{
字符串xml=getUserManagerXml();
字符串metaxml=“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”+“\n”
+“\n”+”;
javax.xml.bind.JAXBContext JAXBContext=JaxbFactory.createJAXBContext(
“com.bitplan.rest.users”,metaxml);
UserManager um4=unmarshalFromContext(jaxbContext,xml);
assertNotNull(um4);
}
@抑制警告(“原始类型”)
@试验
@忽略
public void testUnmarshalViaObjectFactory()引发异常{
字符串xml=getUserManagerXml();
映射属性=新的HashMap();
类[]类={ObjectFactory.Class};
javax.xml.bind.JAXBContext JAXBContext=JAXBContextFactory.createContext(
类别,物业),;
UserManager um3=unmarshalFromContext(jaxbContext,xml);
assertNotNull(um3);
}
@试验
@忽略
public void testUnmarshalViaFromXml()引发异常{
字符串xml=getUserManagerXml();
UserManager um2=UserManagerImpl.fromXml(xml);
assertEquals(2,um2.getUsers().size());
for(用户:um2.getUsers()){
System.out.println(user.getId());
}
用户jd=um2.getById(“jd001”);
jd.deCrypt(um2.getCrypt());
assertEquals(“badpassword”,jd.getPassword());
}