【问题标题】:JAXB compiler is binding xs:long to Java Long instead of primitive long classJAXB 编译器将 xs:long 绑定到 Java Long 而不是原始的 long 类
【发布时间】:2021-07-18 12:37:16
【问题描述】:

我正在从 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>

【问题讨论】:

  • 查看mojohaus.org/jaxb2-maven-plugin/Documentation/v2.5.0/… 以获取可用配置。并且 bindingDirectory 未列出。
  • bindingDirectory 似乎在 2.x 中不可用,替代选项不清楚
  • 您使用 bindingDirectory 的目的是什么?
  • 我假设您之前没有在 Stackoverflow 上搜索过您的问题。因为我在另一个 Stackoverflow 问题中找到了您的答案。 stackoverflow.com/questions/13801401/… 通过寻找 jaxb 生成原语。您必须删除 minOccurs="0" 这表示一个可以为空的值。
  • 您不能将原始类型用于可空值,因为原始类型不可为空。

标签: java jaxb jaxb2-maven-plugin


【解决方案1】:

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

XML Schema type that generates Java primitive type using JAXB doesn't add required to it

但进一步看,我发现了一个 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 compiler is binding xs:boolean to Java Boolean wrapper class, instead of boolean primitive type

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

 generateIsSetMethod = "true"

【讨论】:

    猜你喜欢
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 2021-10-20
    • 2011-11-08
    • 1970-01-01
    • 2013-03-08
    相关资源
    最近更新 更多