JAXB编译器是绑定xs:boolean到Java Boolean包装器类,而不是布尔基本类型

栏目: 服务器 · 编程工具 · 发布时间: 6年前

内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/13035975/jaxb-compiler-is-binding-xsboolean-to-java-boolean-wrapper-class-instead-of-bo

我将项目从JAXB 1.0迁移到JAXB 2.1,并且我在数据类型映射方面遇到问题.

我正在使用Ant xjc绑定编译器,并且我已经成功配置了全局绑定,以便(例如)xs:date映射到java.util.Calendar.

然而,我正在生成返回Boolean的方法,我想要布尔值.

这是复杂的类型:

<xs:element name="usage-auth-rate-charge">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="service-id" type="xs:string"/>
            <xs:element name="pricepoint_custom_fields_required" type="xs:boolean" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

生成的类看起来像这样:

public class UsageAuthRateCharge {
........
public Boolean isPricepointCustomFieldsRequired() {
    return pricepointCustomFieldsRequired;
}

问题是尽管拳击将会起作用,如果提供的XML不包含pricepoint_custom_fields_required的值,则该类的布尔字段为null而不是false.所以我在做这样的事情时得到NullPointerExceptions:

methodWhichTakesPrimitiveBooleanArg(myUsageAuthRateChargeInstance.isPricepointCustomFieldsRequired());

因为它尝试解除传入的布尔值 – 除非它为空.

我无法更改模式,我无法调整所有客户端代码进行空检查.

我已经在我的binding.xml中设置了OptionalProperty属性,如下所示:

<globalBindings optionalProperty="primitive">

在规范中,它说:“如果属性的值是”原始的“,那么它就像在JAXB 1.0”

然而这显然没有发生.

如何解决这个问题?

更新:

现在已经在jaxb 2.2.9中修复了:

https://java.net/jira/browse/JAXB/fixforversion/16850

问题

你得到Boolean而不是boolean的原因是你的元素定义中有minOccurs =“0”.将为缺少的元素存储空值.

<xs:element name="pricepoint_custom_fields_required" type="xs:boolean" minOccurs="0"/>

解决方案应该是什么

该解决方案应该是一个外部绑定文件,它指示一个基本类型应该用于可选的值(参见JAXB 2.2规范的7.5.1).

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">
    <jaxb:globalBindings optionalProperty="primitive"/>
</jaxb:bindings>

外部绑定文件使用XJC的-b选项指定.

xjc -b binding.xml my-schema.xsd

为什么解决方案不起作用

已经打开以下错误以跟踪optionalProperty =“primitive”的问题.

> https://java.net/jira/browse/JAXB-510

> https://java.net/jira/browse/JAXB-926

解决方法

如果您不喜欢在JAXB中生成类的方式,那么您可以自己创建一个类,并将JAXB作为模式的一部分将其作为类生成.

binding.xml

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

    <jxb:bindings schemaLocation="my-schema.xsd">
        <jxb:bindings node="//xs:element[@name='usage-auth-rate-charge']/complexType">
            <jxb:class ref="com.example.foo.UsageAuthRateCharge"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

UsageAuthRateCharge

用你想要的方法创建这个类.

XJC电话

使用XJC调用上的-b标志来指定binding.xml文件

xjc -b binding.xml my-schema.xsd

UPDATE

What I was really hoping was that one of the readers might be a jaxb  dev team member who could do the change easily in jaxb. User  archenroot offered a possible solution in his comments on jaxb issue  927 (a duplicate of 926) which leads me to think that for the right  person, it would be a simple job to fix jaxb

我是 EclipseLink JAXB (MOXy) 领先.我已经接触到JAXB参考实现的同事(他们也适用于Oracle).以下是我从他们那里得到的答复:

I’ve tested in trunk – and the problem exists.  I’ll check code how easy is to fix it.

希望你能够得到一个真正的解决这个问题.

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/13035975/jaxb-compiler-is-binding-xsboolean-to-java-boolean-wrapper-class-instead-of-bo


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Building Websites with Joomla!

Building Websites with Joomla!

H Graf / Packt Publishing / 2006-01-20 / USD 44.99

This book is a fast paced tutorial to creating a website using Joomla!. If you've never used Joomla!, or even any web content management system before, then this book will walk you through each step i......一起来看看 《Building Websites with Joomla!》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具