5

以下は、WebLogic10.3.2バージョンでMOXyJAXB変換を使用してサブクラスを作成するために使用しているクラスです。XMLの生成にEclipseLink2.4.1MOXyを使用しています。次のコードでtype属性を生成できません。ここで何か間違ったことをしている場合はお知らせください。

EclipseLink MOXy2.4.1とWebLogic10.3.2を使用しており、MOXy2.4.1はWebLogicで構成されています

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

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlDiscriminatorNode("@type")
public abstract class BaseEntity {

    private String firstName;
    private String lastName;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

}

サブクラス

package forum13831189;

import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

@XmlDiscriminatorValue("xyz")
public class XyzEntity extends BaseEntity {

    public XyzEntity() {
        super();
    }

}

別のサブクラス

import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

@XmlDiscriminatorValue("Abc")
public class AbcEntity extends BaseEntity {
}

RESTful Webサービスクラス:

@GET
@Path("/xyz")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Representation getAccount() throws CPAException {
    Representation rep = new Representation();
    BaseEntity entity = new XyzEntity();
    entity.setFirstName("first-name");
    entity.setLastName("last-name");
    rep.setEntity(entity);
    return rep;
}

@XmlRootElement
static class Representation {
    private BaseEntity entity;

    public BaseEntity getEntity() {
        return entity;
    }

    public void setEntity(BaseEntity entity) {
        this.entity = entity;
    }
}

上記は次のXMLを生成しています。

<representation>
     <firstName>first-name</firstName>
      <lastName>last-name</lastName>
 </representation>

上記では属性タイプは生成されません。


どうもありがとう。はい、上記のjaxb.propertiesを見逃しました。また、はい、PUTまたはPOSTを使用する場合、XMLが逆シリアル化されると、@ XmlSeeAlsoが存在しない場合、サブクラスを作成できません。

4

1 に答える 1

2

問題を引き起こしている可能性のある項目がいくつかあります。

BaseEntity

デフォルトでは、JAX-RS実装はJAXBContext、サービスメソッドの戻り型またはパラメーター(この場合は)にを作成しますRepresenatation。ドメインモデルを処理するとき、JAXBimplは。などの参照型もプルしますBaseEntity。サブクラスを自動的に取り込むことはできないため、@XmlSeeAlsoアノテーションを使用してサブクラスを参照できます。

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

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlDiscriminatorNode("@type")
@XmlSeeAlso({AbcEntity.class, XyzEntity.class})
public abstract class BaseEntity {

    private String firstName;
    private String lastName;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

}

jaxb.properties

また、@XmlDescriminatorNode/@XmlDescriminatorValueはMOXy拡張機能であるため、JAXBプロバイダーとしてMOXyを指定していることを確認する必要があります。jaxb.propertiesこれは、ドメインモデルと同じパッケージで名前が付けられたファイルを次のエントリで追加することによって行われます。

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

デモ

以下は、RESTfulサービスの機能を模倣したスタンドアロンの例です。

import javax.xml.bind.*;
import javax.xml.bind.annotation.XmlRootElement;

public class Demo {

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

        Representation rep = new Representation();
        BaseEntity entity = new XyzEntity();
        entity.setFirstName("first-name");
        entity.setLastName("last-name");
        rep.setEntity(entity);

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

    @XmlRootElement
    static class Representation {
        private BaseEntity entity;

        public BaseEntity getEntity() {
            return entity;
        }

        public void setEntity(BaseEntity entity) {
            this.entity = entity;
        }
    }

}

出力

以下は、デモコードの実行からの出力です。type属性が存在することを確認してください。

<?xml version="1.0" encoding="UTF-8"?>
<representation>
   <entity type="xyz">
      <firstName>first-name</firstName>
      <lastName>last-name</lastName>
   </entity>
</representation>

詳細については

于 2012-12-12T10:50:20.073 に答える