我正在从1.x版本将一个项目升级到jaxb 2.2.7。
我已经让这个应用程序在某些时候正常工作了,但是在一些回复中我看到了这样的情况:
java.lang.RuntimeException: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: Instance of "com.mycompany.global.er.decoupling.binding.response.PricePointType$BalanceImpactRates$BalanceImpactRate"
is substituting "java.lang.Object", but
"com.mycompany.global.er.decoupling.binding.response.PricePointType$BalanceImpactRates$BalanceImpactRate"
is bound to an anonymous type.]这在jaxb1.0中运行得很好。我不知道会有什么问题。
以下是xsd的摘录(我无法更改,因为客户端正在使用它):
<xs:complexType name="price-pointType">
<xs:sequence>
<xs:element name="id" type="xs:string" />
.........
<xs:element name="duration" type="durationType" />
<xs:element name="order" type="xs:int" />
<xs:element name="min-sub-period" type="xs:int" />
<xs:element name="balance-impacts" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="balance-impact" type="charging-resourceType"
minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="balance-impact-rates" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="balance-impact-rate" minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="rate" type="xs:double" />
</xs:sequence>
<xs:attribute name="charging-resource-code" type="xs:string"
use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>有什么建议吗?
发布于 2014-01-17 09:40:18
结果发现问题是匿名复杂类型的复杂嵌套。
通过按下面的方式把它们分开,问题就解决了。作为额外的奖励,我得到了更多的可重用代码。
<xs:complexType name="balanceImpactRate">
<xs:sequence>
<xs:element name="rate" type="xs:double" />
</xs:sequence>
<xs:attribute name="charging-resource-code" type="xs:string"
use="required" />
</xs:complexType>
<xs:complexType name="balanceImpactRates" >
<xs:sequence>
<xs:element name="balance-impact-rate" type="balanceImpactRate" minOccurs="0"
maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>发布于 2016-02-17 12:23:56
在试图封送一些jaxb服从jaxb时,我也得到了相同的异常,这些jaxb是我从hibernate映射生成的-4.0.xsd。
抛出的异常似乎涉及两个类,它们作为类HibernateMapping根类的内部类生成- "Id“和"CompositeID”。这两个元素在XSD中都被定义为嵌套的complexTypes,就像@mdarwin中的情况一样。通过将complexType定义移出(因此它们是xsd中"schema“元素的根元素),问题就得到了解决,并成功地封送了对象。
遗憾的是,我本来希望使用未经修改的XSD来生成jaxb对象,但却找不到解决问题的其他方法。
发布于 2019-06-12 15:39:05
我提到的情况不依赖于xsd中的<xs:complexType>定义。
实际的问题是,我们有从xml模式生成的类扩展的Java类。
并且用@XmlType(name = "")对这些类进行注释,以使它们是匿名的(即生成的标记不包含xsi:type属性,生成的xml-文件对于初始模式仍然有效。
我在java, xsd & marshalling: jre bug, my fault or xsd issues?中找到了这个解决方案的线索。
由于我无法修改xsd (它太复杂了,已经被API的客户端共享了),所以解决方案是:
package my.existing.class;
public abstract class TheClassFromWhichYouWantToExtend {
}@XmlAnyElement(lax = true)进行注释。 package my.existing.class;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(...)
public class TheClassYouWantToExclude {
// ...
@XmlAnyElement(lax = true)
protected TheClassFromWhichYouWantToExtend theClassFromWhichYouWantToExtend;
}https://stackoverflow.com/questions/21117879
复制相似问题