将其序列化为JSON在MOXy 2.5.0中运行良好:
public Object[] destination_addresses;
但是为了能够使用JAXB(?)将其序列化为XML,我需要一个注释:
@XmlElements({@XmlElement(name = "destination_address", type = LatLong.class),
@XmlElement(name = "destination_address", type = Polygon.class)})
public Object[] destination_addresses;
但是MOXy根本没有将它序列化。如何使用MOXy和JAXB进行序列化?我正在使用Jersey 2.4.1。
MOXy和JAXB按预期工作(JAXB无法序列化LatLong):
@XmlElement(name = "destination_address", type = Polygon.class)
public Object[] destination_addresses;
(我对这一切都很陌生,所以如果我遗漏了有用的信息,请告诉我。)
编辑:不确定为什么MOXy会关心,但这里是LatLong&多边形:
public class LatLong {
public double lat, lng;
}
public class Polygon {
@XmlElement
protected List<LatLong> points = new ArrayList<LatLong>();
public String toString(){return points.toString();}
}
EDIT2:我彻底改变了Polygon以获得我想要的XML:MOXy adds type and uses toString?。现在MOXy抛出:
Exception [EclipseLink-60] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The method [] or [getLatOrLongs] is not defined in the object [com.buzzhives.Routing.Hive.Polygon].
Internal Exception: java.lang.NoSuchMethodException: com.buzzhives.Routing.Hive.Polygon.([Lcom.buzzhives.Routing.Hive.Polygon$LatOrLong;)
Mapping: org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping[latOrLongs]
Descriptor: XMLDescriptor(com.buzzhives.Routing.Hive.Polygon --> [])
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:689)
EDIT3:我已将Polygon
置于原来的状态(即忽略了EDIT2),在MOXy中发现了一个无关的错误(请参阅下面的评论),并更改了{{1} } destination_addresses
代替List<Object>
。 Object[]
现在已经序列化了(使用Polygon
),但简单的points
根本没有序列化。
EDIT4:调试MOXy非常痛苦。我发现它将LatLong
和LatLong
序列化为两个名为Polygon
的独立数组,而不是将它们放在一个数组中。