[英]How to let JAXB render boolean as 0 and 1, not true and false
[英]JAXB Boolean with TRUE/FALSE in uppercase returning null
我在Java 8上使用Apache CXF 2.7.11实现的一些Java优先SOAP服务存在问题。当将TRUE作为值传递时,值变为null。 当小写传递true时,同样有效。
我看到另一个线程,其中扩展XmlAdapter<String, Boolean>
与@XmlJavaTypeAdapter
一起解决了该问题,但是生成的WSDL类型已从xs:boolean更改为xs:string。 这意味着布尔值应由使用者作为字符串传递。 在我的情况下这是不可接受的。
JAXB类:
@XmlRootElement(name = "myPojo")
public class MyPojo {
@XmlElement(name = "myField")
private Boolean myBoolean;
@XmlJavaTypeAdapter(CustomBooleanAdapter.class)
public void setMyBoolean(Boolean bool) {
myBoolean = bool;
}
public Boolean getMyBoolean() {
return myBoolean;
}
}
生成的WSDL类型
<xs:complexType name="myPojo">
<xs:sequence>
<xs:element minOccurs="0" name="myField" type="xs:string"/>
</xs:sequence>
</xs:complexType>
现在,当我在该服务上运行WSDL2Java时,生成的类myField将创建为String而不是Boolean。
有什么办法可以在生成的WSDL中保持类型不变?
当将TRUE作为值传递时,值证明为空。
那是对的。 TRUE不是XML中布尔值的有效值。 请参阅XML模式规范 :
3.2.2.1词汇表示
定义为boolean的数据类型的实例可以具有以下合法文字{ true,false,1,0 }。
如果您使用CustomBooleanAdapter(我认为它看起来像这样),
public static class CustomBooleanAdapter extends XmlAdapter<String, Boolean> {
@Override
public Boolean unmarshal(String s) throws Exception {
return "TRUE".equals(s);
}
@Override
public String marshal(Boolean b) throws Exception {
if (b) {
return "TRUE";
}
return "FALSE";
}
}
您将在规范之上定义自定义逻辑。 适配器将String转换为布尔值,这就是为什么将字段生成为字符串的原因(它甚至在XmlAdapter<String, Boolean>
表示String)。
原则上,您要使用的XmlAdapter<Boolean,Boolean>
不能与大写值“ TRUE”一起使用。
因此,要么使用xs:string和TRUE,要么使用xs:boolean和true。 我真的认为您应该使用规范中的正确布尔值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.