Moxy JAXB和接口

时间:2016-06-18 18:23:05

标签: jaxb moxy

关于如何使用Moxy和JaxB接口

进行了一些讨论

见例如。

Blaise Doughan的答案部分被接受,部分不被接受。 我试图调整

中的答案

https://github.com/BITPlan/com.bitplan.simplerest

参见

https://github.com/BITPlan/com.bitplan.simplerest/blob/master/src/test/java/com/bitplan/jaxb/TestJaxbFactory.java

测试用例。当时有一个

@Ignore

添加到测试用例中。如果我取消注释@Ignore,我会得到不同的错误,具体取决于我尝试的解组方法:

1。使用xml META描述错误是:

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 

2。使用ObjectFactory方法得到:

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)

第3。使用适用于Customer / Order i的JaxbFactory方法:

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

  @Test
  @Ignore
  public void testUnMarshalViaMetaXML() throws Exception {
    String xml = getUserManagerXml();
    String metaxml = "<?xml version=\"1.0\"?>\n"
        + "<xml-bindings\n"
        + "    xmlns=\"http://www.eclipse.org/eclipselink/xsds/persistence/oxm\"\n"
        + "    package-name=\"com.bitplan.rest.users\">\n"
        + "    <xml-schema\n"
        + "        namespace=\"http://www.example.com/user\"\n"
        + "        element-form-default=\"QUALIFIED\"/>\n"
        + "    <java-types>\n"
        + "            <java-type name=\"UserManagerImpl\">\n"
        + "            <xml-root-element name=\"UserManager\"/>\n"
        + "            <java-attributes>\n"
        + "                <xml-element java-attribute=\"users\">\n"
        + "                   <xml-element-wrapper name=\"users\"/>\n"
        + "                </xml-element>\n"
        + "            </java-attributes>\n"
        + "        </java-type>\n"
        + "        <java-type name=\"UserImpl\">\n"
        + "            <xml-root-element name=\"User\"/>\n"
        + "            <xml-type prop-order=\"id name firstname email password comment \"/>\n"
        + "            <java-attributes>\n"
        + "                <xml-element java-attribute=\"name\" name=\"name\"/>\n"
        + "                <xml-element java-attribute=\"firstName\" name=\"firstName\"/>\n"
        + "                <xml-element java-attribute=\"email\" name=\"email\"/>\n"
        + "                <xml-element java-attribute=\"password\" name=\"password\"/>\n"
        + "                <xml-element java-attribute=\"comment\" name=\"comment\"/>\n"
        + "            </java-attributes>\n" + "        </java-type>\n"
        + "    </java-types>\n" + "</xml-bindings>";
    javax.xml.bind.JAXBContext jaxbContext = JaxbFactory.createJAXBContext(
        "com.bitplan.rest.users", metaxml);
    UserManager um4 = unmarshalFromContext(jaxbContext, xml);
    assertNotNull(um4);
  }

  @SuppressWarnings("rawtypes")
  @Test
  @Ignore
  public void testUnmarshalViaObjectFactory() throws Exception {
    String xml = getUserManagerXml();
    Map<String, Object> properties = new HashMap<String, Object>();
    Class[] classes = { ObjectFactory.class };
    javax.xml.bind.JAXBContext jaxbContext = JAXBContextFactory.createContext(
        classes, properties);
    UserManager um3 = unmarshalFromContext(jaxbContext, xml);
    assertNotNull(um3);
  }

  @Test
  @Ignore
  public void testUnmarshalViaFromXml() throws Exception {
    String xml = getUserManagerXml();
    UserManager um2 = UserManagerImpl.fromXml(xml);
    assertEquals(2, um2.getUsers().size());
    for (User user : um2.getUsers()) {
      System.out.println(user.getId());
    }
    User jd = um2.getById("jd001");
    jd.deCrypt(um2.getCrypt());
    assertEquals("badpassword", jd.getPassword());
  }

0 个答案:

没有答案