Jaxb 编组一个 Point 变量

Jaxb Marshalling a Point variable

所以我开始使用 Jaxb 创建我在程序中使用的文件的 XML 副本。

在我的代码中实施后我没有遇到任何问题,直到我最近向我的主 class 添加了一个新的 class。

它 saves/loads 与 class 没问题是 blank/unused,但是当我试图用一些数据编组 class 时我得到 WhosebugErrors。 (在一种情况下,它会毫无问题地编组??)

错误信息

Exception in thread "AWT-EventQueue-0" java.lang.WhosebugError
    at sun.reflect.Reflection.quickCheckMemberAccess(Reflection.java:84)
    at java.lang.reflect.Field.get(Field.java:388)
    at com.sun.xml.internal.bind.v2.runtime.reflect.Accessor$FieldReflection.get(Accessor.java:250)
    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:118)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:345)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:681)
    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:143)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:345)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:681)
    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:143)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:345)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:681)

输出的 .xml 文件在遇到我添加的新 class 之前看起来很正常,这就是它出错的地方。

<positionPercentage>
                        <x>0</x>
                        <y>0</y>
                        <location>
                            <x>0</x>
                            <y>0</y>
                            <location>
<x>0</x>
<y>0</y>
<location>
    <x>0</x>
    <y>0</y>
    <location>
        <x>0</x>
        <y>0</y>
        <location>
            <x>0</x>
            <y>0</y>
            <location>
                <x>0</x>
                <y>0</y>
                <location>
                    <x>0</x>
                    <y>0</y>
                    <location>
                        <x>0</x>
                        <y>0</y>
                        <location>
                            <x>0</x>
                            <y>0</y>
                            <location>

通常 ws class 的 xml 看起来像这样。它包含一个Map<String, WidgetLink> WidgetLinks。这将一个键串映射到一个 WidgetLinkWidgetLink class 包含一个WidgetCodePoint positionPercentage 和两个字符串。点变量好像是程序的问题

<ws>
        <widgetLinks>
            <entry>
                <key>Main-Comp Rack Fault `%rackname`</key>
                <value>
                    <widgetCode>
                        <widgetName>LED-Circle-25x25</widgetName>
                        <variables>
                            <entry>
                                <key>`%XPOS%`</key>
                                <value></value>
                            </entry>
                            <entry>
                                <key>`%YPOS%`</key>
                                <value></value>
                            </entry>
                            <entry>
                                <key>`%IO_ID%`</key>
                                <value></value>
                            </entry>
                        </variables>
                        <fullWidgetText> ALOT OF CODE CUT OUT BUT IT PRINTS FINE</fullWidgetText>
                        <filePath>LED-Circle-25x25.txt</filePath>
                    </widgetCode>
                    <positionPercentage>
                        <x>0</x>
                        <y>0</y>
                    </positionPercentage>
                    <variableName>Comp Rack Fault `%rackname`</variableName>
                    <panelName>Main</panelName>
               </widgetLinks>
           </ws>

我不知道 <location> 物品是什么,或者它来自哪里?

我所有的 classes 使用 @XmlAccessorType(XmlAccessType.FIELD) 而我的主要 class Store 使用代码 @XmlRootElement(name = "Store") @XmlAccessorType(XmlAccessType.FIELD)

错误是否与 Point 变量有关?

编辑 - 我将 Point 变量设置为静态变量,因此不会使用 XmlAccessType.FIELD 的当前设置写入它,并且它写得很好。我怎样才能编组一个点变量?

好的,所以我找到了一个页面来解释为什么 Point 变量会导致这些无限循环错误 -> WhosebugError

Sometimes the best way to handle an unmappable class is to write a "stand-in" class which can be mapped with JAXB, and convert between the two classes in the XmlAdapter. In this example, we want to use the Point class. Because of that class' getLocation() method (which JAXB will pickup automatically and map), an infinite loop will occur during marshalling. Because we cannot change the Point class, we will write a new class, MyPoint, and use it in the adapter. - See more at: http://www.eclipse.org/eclipselink/documentation/2.4/moxy/advanced_concepts006.htm#sthash.A8OXPKIV.dpuf