Java 贾克斯贝伦<;布尔值>;vs布尔

Java 贾克斯贝伦<;布尔值>;vs布尔,java,web-services,soap,jaxb,boolean,Java,Web Services,Soap,Jaxb,Boolean,什么是JAXBElement Boolean,如何将其设置为与“true”等效的布尔值 方法: public void setIncludeAllSubaccounts(JAXBElement<Boolean> paramJAXBElement) { this.includeAllSubaccounts = paramJAXBElement; } 当JAXB(JSR-222)实现无法仅基于该值判断要做什么时,将生成一个JAXBElement,作为模型的一部分。在

什么是
JAXBElement Boolean
,如何将其设置为与“
true
”等效的布尔值

方法:

  public void setIncludeAllSubaccounts(JAXBElement<Boolean> paramJAXBElement)
  {
    this.includeAllSubaccounts = paramJAXBElement;
  }

当JAXB(JSR-222)实现无法仅基于该值判断要做什么时,将生成一个
JAXBElement
,作为模型的一部分。在您的示例中,可能有如下元素:


生成的属性不能是
boolean
,因为
boolean
不表示
null
。您可以将属性
设置为布尔值
,但是如何区分缺少的元素和使用
xsi:nil设置的元素。这就是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;

}
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);
    }

}
输出

<?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>

假的

多亏了NullUserException的评论,我才能够在一行代码中实现这一点。它有点不同,所以我想我会为了其他人的利益而发布它

returnMessageFilter.setIncludeAllSubaccounts(new JAXBElement<Boolean>(new QName("IncludeAllSubaccounts"), 
Boolean.TYPE, Boolean.TRUE));
编辑

<?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>

最好使用
ObjectFactory
类中的便利方法,该方法按照Blaise的建议返回
JAXBElement

如果该方法需要一个布尔值,为什么要尝试传递
true
,一个布尔值?能否包含出现问题的代码和堆栈跟踪?创建一个JAXBElement,例如:
JAXBElement-jaxtrue=newjaxbelement(qname,Boolean.TYPE,Boolean.TRUE)然后传过去好吧,我已经算明白了。Thank.FYI-作为您生成的模型的一部分,应该有一个
ObjectFactory
类,该类包含一个方便的方法,可以使用适当的
QName
创建必要的
JAXBElement
。哦,是的,有。我需要从蓝色中创造一个,这似乎是不对的。非常感谢。
returnMessageFilter.setIncludeAllSubaccounts(new JAXBElement<Boolean>(new QName("IncludeAllSubaccounts"), 
Boolean.TYPE, Boolean.TRUE));
import javax.xml.bind.JAXBElement;