首页 > 解决方案 > JAXB 编译器将 xs:long 绑定到 Java Long 而不是原始的 long 类

问题描述

我正在从 WSDL 生成源代码。它生成包装器类型。我希望 JAXB 生成原始类型而不是包装器。

在 pom.xml 中

                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.5.0</version>
                <executions>
                    <execution>
                        <id>generate-stubs</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <sourceType>wsdl</sourceType>
                            <sources>
                                <source>
                                    ${project.basedir}/src/main/resources/wsdl/my.wsdl
                                </source>
                            </sources>
                            <clearOutputDir>true</clearOutputDir>
                        </configuration>
                    </execution> 
</plugin>

在我的 WSDL 文件中

<xs:element minOccurs="0" name="initBal" type="xs:long"/>

在生成的类中

protected Long initBal;

我试图设置一个 bindingDirectory 来强制使用原语而不是包装器。但似乎该选项不适用于 jaxb2-maven-plugin 2.5.0 版本。

<configuration>
                            <sourceType>wsdl</sourceType>
                            <sources>
                                <source>
                                    ${project.basedir}/src/main/resources/wsdl/my.wsdl
                                </source>
                            </sources>
                            <clearOutputDir>true</clearOutputDir>
                            <xjbSources>
                                <xjbSource>${project.basedir}/src/main/resources/wsdl/aBindingConfiguration.xjb</xjbSource>
                            </xjbSources>
                        </configuration>

aBindingConfiguration.xjb 文件

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" version="2.1">
    <jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws">
        <jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
    </jaxws:bindings>
</jaxb:bindings>

标签: javajaxbjaxb2-maven-plugin

解决方案


通常,您不能将原始类型用于可空值,因为原始类型不可为空。您必须删除 minOccurs="0" ,它表示一个可为空的值。

看看使用 JAXB 生成 Java 原始类型的 XML Schema 类型没有添加到它

但进一步看,我发现了一个 JAXB 绑定设置,官方自定义 JAXB 绑定https://docs.oracle.com/javase/tutorial/jaxb/intro/custom.html没有描述

optionalProperty="primitive"

查看https://download.oracle.com/javaee-archive/jaxb.java.net/users/2011/05/10121.htmlJAXB 编译器将 xs:boolean 绑定到 Java 布尔包装类,而不是布尔原始类型

您也可以尝试使用 JAXB 绑定获取其他方法来询问值状态

 generateIsSetMethod = "true"

推荐阅读