为什么 JAXB generateElementProperty=false 没有达到预期的效果?

2022-01-19 00:00:00 java jaxb

我正在使用以下绑定文件运行 wsimport 任务:

I'm running a wsimport task with the following bindings file:

<jaxb:bindings version="2.1"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
    <jaxb:bindings>
        <jaxb:globalBindings generateElementProperty="false" typesafeEnumMaxMembers="2000" />
    </jaxb:bindings>
</jaxb:bindings>

但是,这会导致类具有 JAXBElement 而不是 String,如下面的 getUserSummaryOrTypeOrLogLevel() 所示

However, this is resulting in classes with JAXBElement<String> instead of String as shown in the getUserSummaryOrTypeOrLogLevel() below

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ConfigSLMAction", propOrder = {
    "userSummaryOrTypeOrLogLevel"
})
public class ConfigSLMAction
    extends ConfigConfigBase
{

    @XmlElementRefs({
        @XmlElementRef(name = "UserSummary", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "LogLevel", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "Type", type = JAXBElement.class, required = false)
    })
    protected List<JAXBElement<String>> userSummaryOrTypeOrLogLevel;
    @XmlAttribute(name = "name")
    protected String name;
    @XmlAttribute(name = "local")
    protected Boolean local;
    @XmlAttribute(name = "intrinsic")
    protected Boolean intrinsic;
    @XmlAttribute(name = "read-only")
    protected Boolean readOnly;
    @XmlAttribute(name = "external")
    protected Boolean external;

    /**
     * Gets the value of the userSummaryOrTypeOrLogLevel property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the userSummaryOrTypeOrLogLevel property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getUserSummaryOrTypeOrLogLevel().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * 
     * 
     */
    public List<JAXBElement<String>> getUserSummaryOrTypeOrLogLevel() {
        if (userSummaryOrTypeOrLogLevel == null) {
            userSummaryOrTypeOrLogLevel = new ArrayList<JAXBElement<String>>();
        }
        return this.userSummaryOrTypeOrLogLevel;
    }
    ...
    ...
    ...
}

生成该类的 xsd 文件中的条目如下:

The entry in the xsd file that this class is being generated from is below:

<xsd:complexType name="ConfigSLMAction">
    <xsd:complexContent>
        <xsd:extension base="tns:ConfigConfigBase">
            <xsd:choice maxOccurs="unbounded">
                <xsd:element name="UserSummary" minOccurs="0" maxOccurs="1">
                    <xsd:simpleType>
                        <xsd:union memberTypes="tns:dmString tns:dmEmptyElement" />
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="Type" minOccurs="1" maxOccurs="1">
                    <xsd:simpleType>
                        <xsd:union memberTypes="tns:dmSLMActionType tns:dmEmptyElement" />
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="LogLevel" minOccurs="0" maxOccurs="1">
                    <xsd:simpleType>
                        <xsd:union memberTypes="tns:dmLogLevel tns:dmEmptyElement" />
                    </xsd:simpleType>
                </xsd:element>
            </xsd:choice>
            <xsd:attributeGroup ref="tns:ConfigAttributes" />
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

这是我的 pom 文件中的 Maven 插件

Here's the Maven plugin from my pom file

<plugin>
    <groupId>org.jvnet.jax-ws-commons</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <id>wsimport-from-jdk</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>wsimport</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <wsdlFiles>
            <wsdlFile>${basedir}/src/main/resources/wsdl/xml-mgmt.wsdl</wsdlFile>
        </wsdlFiles>
        <bindingFiles>
            <bindingFile>${basedir}/src/main/resources/wsdl/bindings.xml</bindingFile>
        </bindingFiles>
        <keep>true</keep>
        <verbose>true</verbose>
        <extension>true</extension>
        <vmArgs>
            <vmArg>-Djavax.xml.accessExternalDTD=all</vmArg>
            <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
        </vmArgs>
    </configuration>
</plugin>

谁能解释为什么会这样?我希望使用 String 而不是 JAXBElement<String> 并且我在 SO 和其他地方找到的任何东西都表明 generateElementProperty=false 有效,但是不是.

Can anyone shed some light on why this is happening? I want String to be used instead of JAXBElement<String> and anything I've found on SO and elsewhere suggests that generateElementProperty=false work but it isn't.

推荐答案

如果存在 xsd:choice 其中 foobar 元素可以出现并且它们是相同的类型.一个简单的字符串不足以标记应编组的元素.

The JAXBElement is mandatory if there is a xsd:choice where either foo or bar elements can occur and they are the same type. A simple String isn't enough to mark which element should be marshalled.

JAXBElement 如果有元素 nillable="true"minOccurs="0" 或者如果有两个全局元素,也是必需的具有相同名称的 xsd:complexType.

JAXBElement is required also if there are element nillable="true" and minOccurs="0" or if there are two global elements with the same named xsd:complexType.

相关文章