java – JAXBElement vs boolean

What exactly is a jaxbelement Boolean value and how to set it to Boolean equivalent to "true"?

method:

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

This does not compile:

returnMessageFilter.setIncludeAllSubaccounts(true);

Solution

When the JAXB (jsr-222) implementation cannot judge the operation to be performed only according to the value, jaxbelement will be generated as part of the model In your example, you might have an element, such as:

<xsd:element 
    name="includeAllSubaccounts" type="xsd:boolean" nillable="true" minOccurs="0"/>

The generated property cannot be Boolean because Boolean values do not represent null You can set the attribute to Boolean, but how to distinguish between missing elements and element sets using XSI: nil This is where jaxbelement comes in See the complete example below:

rich

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 for

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"),value);
    }

    @XmlElementDecl(name="setToValue")
    public JAXBElement<Boolean> createSetToValue(Boolean value) {
        return new JAXBElement(new QName("setToValue"),value);
    }

}

demonstration

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

}

yield

<?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>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>