java - 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>
解决方案
通常,您不能将原始类型用于可空值,因为原始类型不可为空。您必须删除 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.html和 JAXB 编译器将 xs:boolean 绑定到 Java 布尔包装类,而不是布尔原始类型
您也可以尝试使用 JAXB 绑定获取其他方法来询问值状态
generateIsSetMethod = "true"
推荐阅读
- powershell - Powershell使用文件夹/子文件夹树导入CSV并复制到新目录而不复制
- python - Python Github3 库如何获取 ShortOrganization 生成器的确切内容
- c++ - How can deleting a void pointer do anything other than invoke the global delete operator?
- acumatica - Acumatica 编号序列 - 不能默认为新符号
- c# - 在 C# 插件中调用 Dynamics Web API
- swift - 为什么这个 Switch 语句需要为“Switch Value”和“Compound case”加上括号?
- android - 适配器只显示第一项
- node.js - Mongoose:填充引用的子文档
- python - 根据记录的日期绘制数据
- opentok - Opentok Javascript:自我会话/连接断开?