4

バックグラウンド

Attachment という JAR ファイルにクラスがあります。定義の重要な部分を以下に示します。

public class Attachment
{
    public List<Media> media;

    public List<Media> getMedia()
    {
        return this.media;
    }

    public void setMedia(List<Media> media)
    {
        this.media = media;
    }
}

JAXB-impl 2.1.3を使用して、次のコードでこれを逆シリアル化しようとしています。

JAXBContext jaxbContext = JAXBContext.newInstance(Attachment.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

StringReader reader = new StringReader(text);
Attachment attachment = (Attachment) unmarshaller.unmarshal(reader);

ただし、これにより次のエラーが発生します(簡潔にするために省略されています)

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 
1 counts of IllegalAnnotationExceptions

Class has two properties of the same name "media"
    this problem is related to the following location:
        at public java.util.List com.thirdparty.Attachment.getMedia()
            ...
    this problem is related to the following location:
        at public java.util.List com.thirdparty.Attachment.media
            ...

問題は、デフォルトで、JAXB が次のように定義されているPUBLIC_MEMBERのアクセス タイプを使用することであることを理解しています。

XmlTransient によって注釈が付けられていない限り、すべての public getter/setter ペアとすべての public フィールドは自動的に XML にバインドされます。

質問

それで、私の質問は、フィールドを無視してゲッター/セッターだけを使用するように JAXB に指示するにはどうすればよいかということです。無視する必要があるプライベート フィールドがいくつかあるため、この方法を好むことに注意してください (実際には、Attachment が誤ってパブリックに設定されていると思います)。

4

2 に答える 2

2

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

MOXy は、サード パーティのクラスをサポートするための外部マッピング ドキュメント拡張機能を提供します。

サンプル マッピング ドキュメント

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="blog.bindingfile">
    <xml-schema
        namespace="http://www.example.com/customer"
        element-form-default="QUALIFIED"/>
    <java-types>
        <java-type name="Customer">
            <xml-root-element/>
            <xml-type prop-order="firstName lastName address phoneNumbers"/>
            <java-attributes>
                <xml-element java-attribute="firstName" name="first-name"/>
                <xml-element java-attribute="lastName" name="last-name"/>
                <xml-element java-attribute="phoneNumbers" name="phone-number"/>
            </java-attributes>
        </java-type>
        <java-type name="PhoneNumber">
            <java-attributes>
                <xml-attribute java-attribute="type"/>
                <xml-value java-attribute="number"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

外部マッピング ドキュメントからのブートストラップ

以下は、 の作成方法の例ですJAXBContext

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "blog/bindingfile/binding.xml");
        JAXBContext jc = JAXBContext.newInstance("blog.bindingfile", Customer.class.getClassLoader() , properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Customer customer = (Customer) unmarshaller.unmarshal(new File("src/blog/bindingfile/input.xml"));

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

}

詳細については

于 2013-11-05T16:21:04.003 に答える
2

Annoxを見てください。必要なものが揃っているようです。

JAXB 参照実装は、注釈を読み取るための異なる戦略を実装する特別な注釈リーダーで構成できます。Annox はこの機能を利用して、XML から JAXB 注釈をロードできる注釈リーダーを実装します。

于 2013-11-05T16:00:13.990 に答える