6

正確には何でJAXBElement Booleanあり、これをブール値の「true」に設定するにはどうすればよいですか?

方法:

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

これはコンパイルされません:

returnMessageFilter.setIncludeAllSubaccounts(true); 
4

2 に答える 2

8

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. 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. You could make the property Boolean but then how do you distinguish been a missing element and an element set with xsi:nil. This is where JAXBElement comes in. See below for a full example:

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

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>
于 2012-10-03T19:13:28.173 に答える
1

NullUserExceptionのコメントのおかげで、これを1行で実装できました。少し違うので、他の人のために投稿したいと思いました。

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

明確にするために、QNameはXmlElementタグ名です。

また、インポートする必要があります:

import javax.xml.bind.JAXBElement;

編集

Blaiseが提案しObjectFactoryたように、を返すクラスで便利なメソッドを使用することをお勧めします。JAXBElement

于 2012-10-04T17:40:03.127 に答える