久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

JAXB 編譯器將 xs:boolean 綁定到 Java 布爾包裝類,而

JAXB compiler is binding xs:boolean to Java Boolean wrapper class, instead of boolean primitive type(JAXB 編譯器將 xs:boolean 綁定到 Java 布爾包裝類,而不是布爾原始類型)
本文介紹了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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 播放一级毛片 | 91精品国产一区二区在线观看 | 一区二区三区视频 | 91精品国产高清一区二区三区 | av一区二区三区 | 日日夜夜精品视频 | 在线观看不卡av | 欧美8一10sex性hd | 在线视频99| 国产精品欧美精品 | 国产精品久久久久婷婷二区次 | 久久久这里都是精品 | 在线观看国产精品视频 | 欧美一级淫片免费视频黄 | 91在线免费视频 | 国产资源在线播放 | 日韩精品一区二区三区中文在线 | 亚洲视频一区在线播放 | 国产精品久久久久久久久久免费看 | 一区二区日韩 | 精品成人在线 | 欧美不卡在线 | 精品国产欧美一区二区三区成人 | 91九色在线观看 | 亚洲国产成人精品女人久久久 | 日韩欧美一级精品久久 | aaaaaaa片毛片免费观看 | 在线视频一区二区 | 午夜精品一区二区三区免费视频 | 国产精品爱久久久久久久 | 精品久久久一区二区 | 北条麻妃视频在线观看 | 精品伊人 | 精品综合久久 | 一区二区三区福利视频 | 国产成人精品一区二区在线 | 国产探花在线精品一区二区 | 欧美日韩视频网站 | 精品久久久久久 | 精品国产一区二区三区免费 | 国产欧美日韩一区二区三区在线观看 |