有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用@XmlElementRef注释的java元素没有显示在JAXB编组字符串中?

背景

我正在使用一个应用程序,该应用程序使用我们使用jaxws-maven插件检索和生成的数据结构与外部应用程序通信。我们能够接收XML文档,并使用JAXB和它们提供给我们的类结构将它们解组为java对象。但是,当我们将这些对象重新整理成字符串以保存在数据库中以备将来调试时,我们会丢失信息

用于转换的对象片段:

/*the "root" object*/
//XML tags indicating field access, name, and prop order
public class PersonAppResObj {
    @XmlElement(name = "ApplicationObj", required = true)
    protected ApplicationObj application;
    @XmlElement(name = "ContactInfo", required = true)
    protected List<ContactInfo> contactInfo;
    ...
}
//XML tags indicating field access, name, and prop order
public class ApplicationObj extends other.pkg.ApplicationObj {
    @XmlElement(name = "PrimaryApplicant")
    protected List<XObj> primaryApplicant;
    ...
}
package other.pkg
//XML tags indicating field access, name, and prop order
public class ApplicationObj extends GenericApp {
    @XmlElementRef(name="Applicant", namespace="http://pkg.com/1/pkgs", type=JAXBElement.class, required=false)
    protected List<JAXBElement<? extends Applicant>> applicant;
    @XmlElement(name = "ApplicationPrimaryContact", required = true)
    protected XObj applicationPrimaryContact;
    ...
}

以下代码段是在JAXB元素和字符串之间转换的简单Kotlin对象,以及该对象的测试:

object Converter {
    fun xmlToXObj(respString: String): XObj? {
        val jaxbContext = JAXBContext.newInstance(ResponseRoot().javaClass)
        val unmarshaller = jaxbContext.createUnmarshaller()
        unmarshaller.setEventHandler {
            println(it.message)
            true
        }
        return (unmarshaller.unmarshal(ByteArrayInputStream(respString.toByteArray())) as ResponseRoot).xObj
    }
    fun xobjToXML(resp: XObj): String {
        val responseRoot = ResponseRoot(); responseRoot.xobj = resp
        val resMarsh = JAXBContext.newInstance(responseRoot.javaClass).createMarshaller()
        val resStream = ByteArrayOutputStream()
        resMarsh.marshal(responseRoot, resStream)
        return String(resStream.toByteArray())
    }
}
//autowired var feClient fetches info from other application
    @Test
    fun readWrite() {
        val rawResponse = feClient.fetchApp("AA", "1A")
        val xmlString = Converter.xobjToXML(rawResponse)
        val parsedResponse = Converter.xmlToXObj(xmlString)
        val parsedTwiceXML = Converter.xobjToXML(parsedResponse!!)
        println(xmlString)
        println(parsedTwiceXML)
    }

现在,让我们来谈谈问题的实质: 当我运行读写测试时,我丢失了来自other.pkg.ApplicationObjApplicant信息:

//println(xmlString)
<ns4:PersonAppResObj>
            <ns4:ApplicationObj>
                <ns4:Applicant ns1:id="Applicant1">
                ...
                <ns3:PrimaryContact ns1:ref="primaryContact"/>

//println(parsedTwiceXML)
<ns4:PersonAppResObj>
            <ns4:ApplicationObj>
                <ns3:PrimaryContact ns1:ref="primaryContact"/>
                //no applicant to be found

控制台中出现的错误是 unexpected element (uri:"...", local:"Applicant"). Expected elements are <{...}PrimaryFilerPerson>,<{...}JointFilerPerson>,<{...}ApplicationPrimaryContact>,<{...}ApplicationMultipleIndicator>,<{...}Applicant>

主要问题

控制台中出现了两个类似的错误,这些元素也丢失了。当我查看生成的类文件中的那些对象时,没有显示的所有3个元素都用@XmlElementRef注释

首先,是什么导致了这个问题?这只是封送员找不到引用对象的情况吗

第二,有没有一种方法可以在根本不编辑生成的对象的情况下解决此问题?如果没有,在不改变xml外观的情况下我能做什么


共 (0) 个答案