【发布时间】:2017-02-09 08:32:59
【问题描述】:
我正在从 XSD 为我正在为其构建客户端的 SOAP WebService 生成 JAXB 类(使用 jaxws-maven-plugin v2.4.1 生成,wsimport 目标)。
我遇到了一个问题,即在编组我的对象时,JAXB 不会将 xsi:type-Information 添加到抽象类型的节点。 WebService 现在(我认为是正确的)抱怨我试图传递它的元素而不指定它们是什么类型(“类型定义不能是元素的抽象......”)。
这是一个演示我的问题的简化示例:
抽象类型架构: (abstract.xsd)
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.example.com/namespace_abstract"
elementFormDefault="qualified"
attributeFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.com/namespace_abstract">
<xsd:complexType name="ElementType" abstract="true">
<xsd:simpleContent>
<xsd:extension base="xsd:string"/>
</xsd:simpleContent>
</xsd:complexType>
</xsd:schema>
具体类型架构: (concrete.xsd)
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.example.com/namespace_concrete"
elementFormDefault="qualified"
attributeFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.com/namespace_concrete"
xmlns:abstr="http://www.example.com/namespace_abstract">
<xsd:import namespace="http://www.example.com/namespace_abstract" schemaLocation="abstract.xsd"/>
<!-- Concrete type -->
<xsd:complexType name="ElementTypeExtension">
<xsd:simpleContent>
<xsd:restriction base="abstr:ElementType">
<xsd:enumeration value="one"/>
<xsd:enumeration value="two"/>
<xsd:enumeration value="three"/>
</xsd:restriction>
</xsd:simpleContent>
</xsd:complexType>
<!-- Type that has a field of the abstract type -->
<xsd:complexType name="ExtensionTypeContainer">
<xsd:sequence>
<xsd:element name="type" type="abstr:ElementType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
测试:
import com.example.namespace_concrete.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.XmlRootElement;
public class MarshallingTest {
public static void main(String[] args) throws JAXBException {
ElementTypeExtension elementTypeExtension = new ElementTypeExtension();
elementTypeExtension.setValue("one");
ExtensionTypeContainer extensionTypeContainer = new ExtensionTypeContainer();
extensionTypeContainer.setType(elementTypeExtension);
XmlRoot xmlRoot = new XmlRoot();
xmlRoot.setContainer(extensionTypeContainer);
Marshaller marshaller = JAXBContext.newInstance(XmlRoot.class, ExtensionTypeContainer.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(xmlRoot, System.out);
}
@XmlRootElement
static class XmlRoot {
private ExtensionTypeContainer container;
public ExtensionTypeContainer getContainer() { return container; }
public void setContainer(ExtensionTypeContainer container) { this.container = container; }
}
}
输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xmlRoot xmlns:ns2="http://www.example.com/namespace_concrete">
<container>
<ns2:type>one</ns2:type>
</container>
</xmlRoot>
缺少的部分是ns2:type 节点上的xsi:type="ns2:ElementTypeExtension",使类型定义不明确(当然在我的示例中只有一种具体类型,但仍然如此)。
我找到了一种通过修改生成的抽象类的源代码来生成xsi:type 的方法(为了便于阅读,删除了 JAXB-javadoc):
package com.example.namespace_abstract;
import javax.xml.bind.annotation.*;
import com.example.namespace_concrete.ElementTypeExtension;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ElementType", propOrder = { "value" })
@XmlSeeAlso({ ElementTypeExtension.class })
public abstract class ElementType {
@XmlValue
protected String value;
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
}
一旦我从value 字段中删除@XmlValue-Annotation,xsi:type-Information 就会出现在编组的 XML 中。源代码是从 XSD 生成的,所以这不是一个真正的选择。
谁能告诉我如何在其中获取xsi:type?修改 xsd 是一种选择。
【问题讨论】:
-
我针对您的源模式运行了 xjc,您的示例对我有用。生成的
ElementType类也与您问题中的类相同。 -
感谢您对其进行测试,该问题似乎与 JAXB 中的 this 错误有关。将我的 JAXB 版本更新到 2.2.11 修复了它。