1

シナリオ: .NET (WCF) によって作成された Web サービスに接続する Swing アプリを開発しています。いくつか問題がありましたが、現在はほとんど解決されています。ただし、ブール値のプロパティを更新できないようです。私のエンティティには、ブール値のプロパティがありますGender:

True = 男性、

False = 女性、

Null = 不明/未指定。

コード:

updatedEntry.setGender(factory.createBoolean(true));

ここでは、によって私のために生成されたクラスfactoryの単なるインスタンスです。型であり、値が実際に True に設定されていることがわかります。更新メソッドはエラーなしで正常に実行されますが、データベースにその列があることがわかります。他の列が正常に更新されるため、これは非常に奇妙です。ObjectFactoryNetbeansupdatedEntryJAXBElement<Boolean>NULL

Web サービスに問題がないことを確認するために、SoapUIを使用してパラメーターを手動で入力しました。問題なく動作しました。私が気づいたことの 1 つは、string を使用できずTrue、それを機能させるには a を入力する必要がある1ことでした。それは私を不思議に思います。JAXBElementまたは私のプロジェクトから生成された他のコードは、代わりにNetbeansとして値を送信していますか? しかし、そうであれば、SoapUIを試したときにエラーがスローされたのはなぜですか? 私が言ったように..から性別を除くすべてのプロパティが更新されました。true1NetbeansNetbeans

Netbeans によって生成された Soap XML を表示する方法はありますか? または、誰かが何かアイデアを持っていますか...、この問題を以前に見ましたか?

4

1 に答える 1

0

以下は役立つ例です。

XML スキーマ - schema.xsd

と の両方でJAXBElement<Boolean>ある要素がある場合、 type のプロパティを取得します。nillable="true"minOccurs="0"

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/schema"
    xmlns:tns="http://www.example.org/schema" 
    elementFormDefault="qualified">

    <element name="root">
        <complexType>
            <sequence>
                <element name="trueValue" type="boolean" minOccurs="0" nillable="true"/>
                <element name="falseValue" type="boolean" minOccurs="0" nillable="true"/>
                <element name="nullValue" type="boolean" minOccurs="0" nillable="true"/>
                <element name="notSetValue" type="boolean" minOccurs="0" nillable="true"/>
            </sequence>
        </complexType>
    </element>

</schema>

生成されたモデル - ルート

以下は、要素から生成されたクラスがどのrootように見えるかです:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"trueValue", "falseValue", "nullValue", "notSetValue"})
@XmlRootElement(name = "root")
public class Root {

    @XmlElementRef(name = "trueValue", namespace = "http://www.example.org/schema", type = JAXBElement.class, required = false)
    protected JAXBElement<Boolean> trueValue;
    @XmlElementRef(name = "falseValue", namespace = "http://www.example.org/schema", type = JAXBElement.class, required = false)
    protected JAXBElement<Boolean> falseValue;
    @XmlElementRef(name = "nullValue", namespace = "http://www.example.org/schema", type = JAXBElement.class, required = false)
    protected JAXBElement<Boolean> nullValue;
    @XmlElementRef(name = "notSetValue", namespace = "http://www.example.org/schema", type = JAXBElement.class, required = false)
    protected JAXBElement<Boolean> notSetValue;

}

生成されたモデル - ObjectFactory

@XmlRegistry
public class ObjectFactory {

    private final static QName _RootNullValue_QNAME = new QName("http://www.example.org/schema", "nullValue");
    private final static QName _RootTrueValue_QNAME = new QName("http://www.example.org/schema", "trueValue");
    private final static QName _RootFalseValue_QNAME = new QName("http://www.example.org/schema", "falseValue");
    private final static QName _RootNotSetValue_QNAME = new QName("http://www.example.org/schema", "notSetValue");

    public ObjectFactory() {
    }

    public Root createRoot() {
        return new Root();
    }

    @XmlElementDecl(namespace = "http://www.example.org/schema", name = "nullValue", scope = Root.class)
    public JAXBElement<Boolean> createRootNullValue(Boolean value) {
        return new JAXBElement<Boolean>(_RootNullValue_QNAME, Boolean.class, Root.class, value);
    }

    @XmlElementDecl(namespace = "http://www.example.org/schema", name = "trueValue", scope = Root.class)
    public JAXBElement<Boolean> createRootTrueValue(Boolean value) {
        return new JAXBElement<Boolean>(_RootTrueValue_QNAME, Boolean.class, Root.class, value);
    }

    @XmlElementDecl(namespace = "http://www.example.org/schema", name = "falseValue", scope = Root.class)
    public JAXBElement<Boolean> createRootFalseValue(Boolean value) {
        return new JAXBElement<Boolean>(_RootFalseValue_QNAME, Boolean.class, Root.class, value);
    }

    @XmlElementDecl(namespace = "http://www.example.org/schema", name = "notSetValue", scope = Root.class)
    public JAXBElement<Boolean> createRootNotSetValue(Boolean value) {
        return new JAXBElement<Boolean>(_RootNotSetValue_QNAME, Boolean.class, Root.class, value);
    }

}

デモ

JAXBElement<Boolean>以下は、4 つの可能なオプションのそれぞれを示すために、各プロパティを異なる方法で設定するデモ コードです。

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance("forum15170433");

        Root root = new Root();
        ObjectFactory objectFactory = new ObjectFactory();

        // set the value to true
        root.setTrueValue(objectFactory.createRootTrueValue(true));

        // set the value to false
        root.setFalseValue(objectFactory.createRootFalseValue(false));

        // set the value to null
        root.setNullValue(objectFactory.createRootNullValue(null));

        // specify the value is unset
        root.setNotSetValue(null);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

出力

以下は、デモ コードを実行した結果の出力です。xsi:nil属性が値を表すために使用されnull、設定されていない値がマーシャリングされていないことがわかります。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns="http://www.example.org/schema">
    <trueValue>true</trueValue>
    <falseValue>false</falseValue>
    <nullValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</root>
于 2013-03-04T14:29:35.123 に答える