我從XSD爲SOAP WebService生成JAXB類我正在構建一個客戶端(使用jaxws-maven-plugin v2.4.1生成,wsimport目標)。JAXB:編組XML中缺少具體類型信息(xsi:type)
我遇到了一個問題,在編組我的對象時,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"
,離開類型定義不明確(當然在我的例子中,只有一個具體類型,但仍然)。
我發現了一種通過modifiying抽象類的生成的源代碼(除去JAXB-的Javadoc可讀性)以產生xsi:type
:
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
- 信息存在於編組的XML中。源代碼是從XSD生成的,所以這不是一個真正的選擇。
任何人都可以請給我一個關於如何獲得xsi:type
的想法嗎?修改xsd將是一個選項。
我對你的源代碼模式運行了xjc,你的例子爲我工作。生成的'ElementType'類與您的問題中的類相同。 – teppic
感謝您對此進行測試,似乎這個問題與JAXB中的[this](https://java.net/jira/browse/JAXB-890)錯誤有關。更新我的JAXB版本到2.2.11修復了它。 – kwj