JAXB 编译器将 xs:boolean 绑定到 Java 布尔包装类,而

时间:2023-01-14
本文介绍了JAXB 编译器将 xs:boolean 绑定到 Java 布尔包装类,而不是布尔原始类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个项目从 JAXB 1.0 迁移到 JAXB 2.1,但我遇到了数据类型映射问题.

I'm migrating a project from JAXB 1.0 to JAXB 2.1 and I'm having problems with the datatype mapping.

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

I'm using the Ant xjc binding compiler, and I've successfully configured the global bindings such that (for example) xs:date maps to java.util.Calendar.

但是,我得到了返回 Boolean 的生成方法,而我想要 boolean.

However I'm getting generated methods which return Boolean, whereas I want 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:

The problem is that although boxing will work, if the supplied XML doesn't contain a value for pricepoint_custom_fields_required, the class's Boolean field is null, instead of false. So I get NullPointerExceptions when doing something like this:

methodWhichTakesPrimitiveBooleanArg(myUsageAuthRateChargeInstance.isPricepointCustomFieldsRequired());

因为它试图将传入的布尔值拆箱 - 除非它是空值.

because it tries to unbox the Boolean passed in - except it's null.


我无法更改架构,也无法调整所有客户端代码来进行空值检查.


I can't change the schema, and I can't adjust all the client code to do the null checks.

我在 binding.xml 中设置了 optionalProperty 属性,如下所示:

I've set the optionalProperty attribute in my binding.xml as follows:

<globalBindings optionalProperty="primitive">

在规范中,它说:如果属性的值是‘原始的’,它会像在 JAXB 1.0 中那样绑定"

In the spec, it says: "If the attribute’s value is "primitive", it binds as it did in JAXB 1.0"

然而这显然没有发生.

我该如何解决这个问题?

How can I solve this problem?

更新:

这个问题现在在 jaxb 2.2.9 中得到修复:https://java.net/jira/browse/JAXB/fixforversion/16850

This is now fixed in jaxb 2.2.9: https://java.net/jira/browse/JAXB/fixforversion/16850

推荐答案

我厌倦了等待开发团队的修复,所以我卷起袖子自己动手.

I got bored of waiting for a fix from the dev team so I rolled up my sleeves and did it myself.

我将下面的代码包括在内,以帮助那些同样存在问题的人.

I'm including the code below to help people for whom this is also an issue.

免责声明:我的代码可能不是解决问题的最佳方法,但它对我有用.

Disclaimer: my code probably isn't the best way to solve the problem but it works for me.

生成的代码现在如下所示:

The generated code now looks like this:

public boolean isPricepointCustomFieldsRequired() {
    if (pricepointCustomFieldsRequired == null) {
        return false;
    } else {
        return pricepointCustomFieldsRequired;
    }
}

并且修改如下:

com.sun.tools.xjc.reader.xmlschema.bindinfo.BIProperty:createElementProperty, line ~358

com.sun.tools.xjc.reader.xmlschema.bindinfo.BIProperty:createElementProperty, line ~358

types.addTo(prop);

插入以下代码:

if (prop.isOptionalPrimitive() && getOptionalPropertyMode() == OptionalPropertyMode.PRIMITIVE &&
    !prop.getTypes().isEmpty() && "boolean".equals(prop.getTypes().get(0).getTypeName().getLocalPart()) )   {
        prop.defaultValue= CDefaultValue.create(CBuiltinLeafInfo.BOOLEAN, new XmlString("false"));
    }

这篇关于JAXB 编译器将 xs:boolean 绑定到 Java 布尔包装类,而不是布尔原始类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:在 Android 中将 XML 文件解组为 Java 对象? 下一篇:在 XML 模式中指定 IDREF 的类型

相关文章

最新文章