2

出力用にマーシャリングされている単純なクラス:

@XmlRootElement
public class Foobar {
    // ...

    void beforeMarshal(Marshaller m) {
        System.out.println("beforeMarshal fired");
    }
}

JAX-RS も非常に単純です。

@GET
public Response getResponse() {
    Foobar fb = new Foobar();
    // ...
    return Response.ok(fb).build();
}

予想される出力は「beforeMarshal が発射される」1 回ですが、2 回発射されますか?
これは正常ですか?余分なフラグを使用することは良い考えではないと思います..

@XmlTransient
private boolean marshalled;

void beforeMarshal(Marshaller m) {
    if (!this.marshalled) {
        System.out.println("beforeMarshal");
        this.marshalled = true;
    }
}

また、json 出力のリソースを照会する場合、marshal イベントはまったく発生しません。

4

1 に答える 1

1

アップデート

MOXy には、GlassFish などの OSGi 環境でマーシャル/アンマーシャル メソッドの呼び出しを妨げるバグ ( http://bugs.eclipse.org/412417を参照) がありました。これは、EclipseLink 2.3.3、2.4.3、2.5.1、および 2.6.0 ストリームで修正されました。2013 年 7 月 10 日以降、次のリンクからナイトリー ビルドをダウンロードできます。

同じイベントが 2 回呼び出される問題を再現できませんでした。この問題を示すコード例がある場合は、私のブログを通じて私との電子メールでの会話を開始してください。


XMLバインディング

beforeMarshalメソッドが 1 回ではなく 2 回呼び出されている場合は、JAXB (JSR-222) プロバイダーとして EclipseLink MOXy ではなく参照実装を使用します

デモコード

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foobar.class);
        System.out.println(jc.getClass());

        Foobar foobar = new Foobar();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(foobar, System.out);
    }

}

出力 - JAXB リファレンス実装

class com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl
beforeMarshal fired
beforeMarshal fired
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><foobar/>

出力 - EclipseLink MOXy

class org.eclipse.persistence.jaxb.JAXBContext
beforeMarshal fired
<?xml version="1.0" encoding="UTF-8"?>
<foobar/>

MOXy を JAXB プロバイダーとして有効にするにはjaxb.properties、次のエントリを使用してドメイン モデルと同じパッケージに呼び出されるファイルを含める必要があります ( http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-asを参照)。 -your.html )。

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

JSONバインディング

MOXy は、XML と JSON のバインドに同じ配管を使用します。これは、両方で同じイベント動作が見られることを意味します。イベントが表示されない場合は、MOXy 以外の JSON バインディング プロバイダーを使用してください。

于 2013-07-05T14:16:01.420 に答える