[英]JAXBElement<Boolean> vs boolean
What exactly is JAXBElement Boolean
and how can I set this to the boolean equivalent of " true
"? 究竟什么是
JAXBElement Boolean
,如何将其设置为布尔等效于“ true
”?
Method: 方法:
public void setIncludeAllSubaccounts(JAXBElement<Boolean> paramJAXBElement)
{
this.includeAllSubaccounts = paramJAXBElement;
}
This does not compile: 这并不编译:
returnMessageFilter.setIncludeAllSubaccounts(true);
A JAXBElement
is generated as part of your model when a JAXB (JSR-222) implementation would not be able to tell what to do based on the value alone. 当JAXB(JSR-222)实现无法仅根据值来判断要执行的操作时,
JAXBElement
将作为模型的一部分生成。 In your example you probably had an element like: 在您的示例中,您可能有一个元素,如:
<xsd:element
name="includeAllSubaccounts" type="xsd:boolean" nillable="true" minOccurs="0"/>
The generated property can't be boolean
because boolean
doesn't represent null
. 生成的属性不能是
boolean
因为boolean
不表示null
。 You could make the property Boolean
but then how do you distinguish been a missing element and an element set with xsi:nil
. 您可以将属性设为
Boolean
但是如何区分缺少的元素和使用xsi:nil
的元素集。 This is where JAXBElement comes in. See below for a full example: 这就是JAXBElement的用武之地。请参阅下面的完整示例:
Foo 富
package forum12713373;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlElementRef(name="absent")
JAXBElement<Boolean> absent;
@XmlElementRef(name="setToNull")
JAXBElement<Boolean> setToNull;
@XmlElementRef(name="setToValue")
JAXBElement<Boolean> setToValue;
}
ObjectFactory 的ObjectFactory
package forum12713373;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
@XmlElementDecl(name="absent")
public JAXBElement<Boolean> createAbsent(Boolean value) {
return new JAXBElement(new QName("absent"), Boolean.class, value);
}
@XmlElementDecl(name="setToNull")
public JAXBElement<Boolean> createSetToNull(Boolean value) {
return new JAXBElement(new QName("setToNull"), Boolean.class, value);
}
@XmlElementDecl(name="setToValue")
public JAXBElement<Boolean> createSetToValue(Boolean value) {
return new JAXBElement(new QName("setToValue"), Boolean.class, value);
}
}
Demo 演示
package forum12713373;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
ObjectFactory objectFactory = new ObjectFactory();
Foo foo = new Foo();
foo.absent = null;
foo.setToNull = objectFactory.createSetToNull(null);
foo.setToValue = objectFactory.createSetToValue(false);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
}
}
Output 产量
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo>
<setToNull xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<setToValue>false</setToValue>
</foo>
Thanks to NullUserException's comment, I was able implement this in one line. 感谢NullUserException的评论,我能够在一行中实现这一点。 It is slightly different so I thought I'd post it for the benefit of others.
它略有不同,所以我认为我会发布它以造福他人。
returnMessageFilter.setIncludeAllSubaccounts(new JAXBElement<Boolean>(new QName("IncludeAllSubaccounts"),
Boolean.TYPE, Boolean.TRUE));
Just to clarify, the QName is the XmlElement tag name. 为了澄清,QName是XmlElement标记名称。
Also, needed to import: 还需要导入:
import javax.xml.bind.JAXBElement;
Edit 编辑
Better to use the convenience method in ObjectFactory
class that returns the JAXBElement
as Blaise suggested. 最好使用
ObjectFactory
类中的JAXBElement
方法,该方法返回Blaise建议的JAXBElement
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.