java - JAXB:编码的 XML 中缺少具体类型信息 (xsi:type)

标签 java xml xsd jaxb

我正在从 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 是一种选择。

最佳答案

更新到 JAXB 2.2.11 解决了这个问题,似乎与 this JAXB issue 有关.

编辑:给定的 URL 已失效,请检查这个:JAXB Unmarshaller tries to instantiate abstract class ignoring xsi:type if nillable="true" #890

关于java - JAXB:编码的 XML 中缺少具体类型信息 (xsi:type),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42131632/

相关文章:

Java:一行中多个++增加。第一个是哪一个?

php - 访问 xml 对象

c# - WriteStartDocument 编写的错误 XML 编码标记

xml - Visual Studio 2010 中的 XSD key/keyref intellisense 验证支持

java - 即使模板文件位于同一目录中,apache 速度资源未找到异常

java - 如何将 RecyclerView 中显示的数据与 Firebase 中的文档关联

java - 为什么我无法与 iframe 中的元素交互?

php - 服务器接收文件的响应是自动的还是必须手动编码?

java - 如何使用 JAXB (Intellij Idea) 从多个 xsd 生成 Java 类

xml - 在 ISS-UI AATS 2016 网站上寻找示例 IRS AIR XML 文件