Java 封送的XML上缺少JAXB:具体类型信息(xsi:type)
我正在从XSD为一个SOAP Web服务生成JAXB类,我正在为其构建一个客户端(使用jaxws maven插件v2.4.1,wsimport目标生成) 我遇到了一个问题,JAXB在编组对象时不会向抽象类型的节点添加xsi:type信息。WebService now(我认为这是正确的)抱怨说,我试图传递它的元素,但没有指定它们是什么类型(“元素的类型定义不能是抽象的…”) 下面是一个简单的例子来说明我的问题: 抽象类型架构:(Abstract.xsd) 输出:Java 封送的XML上缺少JAXB:具体类型信息(xsi:type),java,xml,xsd,jaxb,Java,Xml,Xsd,Jaxb,我正在从XSD为一个SOAP Web服务生成JAXB类,我正在为其构建一个客户端(使用jaxws maven插件v2.4.1,wsimport目标生成) 我遇到了一个问题,JAXB在编组对象时不会向抽象类型的节点添加xsi:type信息。WebService now(我认为这是正确的)抱怨说,我试图传递它的元素,但没有指定它们是什么类型(“元素的类型定义不能是抽象的…”) 下面是一个简单的例子来说明我的问题: 抽象类型架构:(Abstract.xsd) 输出: import com.exampl
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>
从value
字段中删除@XmlValue
-注释后,xsi:type
-信息将出现在封送的XML中。但是源代码是从XSD生成的,所以这实际上不是一个选项
有谁能告诉我如何在那里输入
xsi:type
?修改xsd将是一个选项。更新到JAXB 2.2.11解决了这个问题,似乎与此有关。我针对您的源模式运行了xjc,您的示例对我有效。生成的ElementType
类也与您问题中的类相同。感谢您的测试,似乎问题与JAXB中的bug有关。将我的JAXB版本更新到2.2.11修复了它。
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>
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; }
}