問題描述
我正在將一個項目從 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模板網!