2

私は次のクラスを持っています

@XmlRootElement(name = "entity")
public class Entity {

    @XmlElementRef
    protected AtomLink first;
    @XmlElementRef
    protected AtomLink second;

    public Entity() {
    }

    public Entity(AtomLink first, AtomLink second) {
        this.first = first;
    this.second = second;
    }
}

これは私のテストコードです:

Entity entity = new Entity(new AtomLink("first", "http://test/first"), new AtomLink("second", "http://test/second"));
JAXBContext context;
try {
    context = JAXBContextFactory.createContext(new Class[] { Entity.class } , null);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(entity, System.out);
} catch (JAXBException e) {
    e.printStackTrace();
}

最初のリンクが欠落しているため、MOXyの出力は正しくありません。

<entity xmlns:atom="http://www.w3.org/2005/Atom">
    <atom:link rel="second" href="http://test/second"/>
</entity>

JavaJAXBRIでの出力は正しいです。

<entity xmlns:atom="http://www.w3.org/2005/Atom">
    <atom:link rel="first" href="http://test/first"/>
    <atom:link rel="second" href="http://test/second"/>
</entity>

MOXyのバグですか?

4

1 に答える 1

1

注: 私はEclipseLink JAXB(MOXy)のリーダーであり、JAXB(JSR-222)エキスパートグループのメンバーです。

MOXyのバグですか?

いいえ、そうではありません。問題は、同じタイプの2つのプロパティの両方に。の注釈を付けることは無効であるということです@XmlElementRef。JAXB RIを使用してマーシャリングを解除した場合:

<entity xmlns:atom="http://www.w3.org/2005/Atom">
    <atom:link rel="first" href="http://test/first"/>
    <atom:link rel="second" href="http://test/second"/>
</entity>

そして、それを元に戻し、MOXyのように次のようになります。

<entity xmlns:atom="http://www.w3.org/2005/Atom">
    <atom:link rel="second" href="http://test/second"/>
</entity>

回避策-任意のJAXB(JSR-222)の実装

JAXBでは、繰り返し要素はコレクションプロパティで表す必要があります。

@XmlRootElement
public class Entity {

    @XmlElementRef
    protected List<AtomLink> first;

    public Entity() {
    }

}

MOXyの使用@XmlPath

以下は、MOXyの@XmlPath拡張機能を活用してこのユースケースをサポートする方法の例です。

package-info

@XmlSchemaアノテーションで次の名前空間情報が指定されていると仮定します。

@XmlSchema(
    xmlns={
       @XmlNs(prefix="atom", namespaceURI="http://www.w3.org/2005/Atom")
    }
)
package forum14998000;

import javax.xml.bind.annotation.*;

実在物

次に、を使用@XmlPathして、フィールドを特定の属性値を持つ要素にマップできます。要素名/URI以上に一致しているので、元の問題にぶつかることはありません。

package forum14998000;

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name = "entity")
public class Entity {

    @XmlPath("atom:link[@rel='first']")
    protected AtomLink first;

    @XmlPath("atom:link[@rel='second']")
    protected AtomLink second;

    public Entity() {
    }

    public Entity(AtomLink first, AtomLink second) {
        this.first = first;
    this.second = second;
    }

}

AtomLink

属性がアノテーションrelでカバーされているので、クラス@XmlPathのフィールドとして属性を含めません。AtomLink

import javax.xml.bind.annotation.*;

@XmlRootElement(namespace="http://www.w3.org/2005/Atom", name="link")
@XmlAccessorType(XmlAccessType.FIELD)
public class AtomLink {

    @XmlAttribute
    private String href;

}

詳細については

于 2013-02-21T09:54:29.467 に答える