21

私は JAXB を自分のグルーヴィーなクラスで動作させようとしていますが、動作していないように見えますが、Java バージョンでは動作します。これがコードです...

シナリオは次のとおりです。

2 と 3 のコメントを外せば問題なく動作します。

1 と 4 のコメントを外すと、次のようになります。

 com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:
       2 counts of IllegalAnnotationExceptions
 groovy.lang.MetaClass is an interface, and JAXB can't handle interfaces.

1 と 5 のコメントを外すと、次のようになります。

  javax.xml.bind.JAXBException: class org.oclc.presentations.simplejaxb.PlayerGroovy
        nor any of its super class is known to this context.

何か案は?

ジャワ:

    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
    public class Player {
    }

グルーヴィー:

    import javax.xml.bind.annotation.XmlRootElement

    @XmlRootElement
    public class PlayerGroovy {
    }

テスト:

    import org.junit.Test
    import javax.xml.bind.JAXBContext
    import javax.xml.bind.Marshaller
    import org.junit.Assert

    class PlayerTest {
        @Test
        public void testJaXB(){
            //1 PlayerGroovy player = new PlayerGroovy()
            //2 Player player = new Player()
            StringWriter writer = new StringWriter();
            //3 JAXBContext context = JAXBContext.newInstance(Player.class);
            //4 JAXBContext context = JAXBContext.newInstance(PlayerGroovy.class);
            //5 JAXBContext context = JAXBContext.newInstance(PlayerGroovy.getClass());
            Marshaller m = context.createMarshaller();
            m.marshal(player, writer);
            println(writer)
            Assert.assertTrue(true)
        }
    }
4

3 に答える 3

22

1 と 4 のコメントを外すのが、Groovy で JAXB を設定する正しい方法です。機能しない理由は、各 Groovy クラスに metaClass プロパティがあるためです。JAXB はこれを明らかに失敗する JAXB プロパティとして公開しようとしています。metaClass プロパティを自分で宣言しないため、注釈を付けて JAXB に無視させることはできません。代わりに、XmlAccessType を NONE に設定します。これにより、JAXB によるプロパティの自動検出が無効になり、XML 要素として公開されます。その後、公開するフィールドを明示的に宣言する必要があります。

例:

@XmlAccessorType( XmlAccessType.NONE )
@XmlRootElement
public class PlayerGroovy {
    @XmlAttribute
    String value
}
于 2009-07-24T14:10:00.627 に答える
16

Grails GORM オブジェクトを公開しているときに同じ問題が発生しました。を使用して上記の解決策を調査した後、@XmlAccessorType( XmlAccessType.NONE )すべてを としてマークすることにすぐにうんざりしました@XmlAttribute

私は以下を使用して多くの成功を収めています:

@XmlAccessorType( XmlAccessType.FIELD )
@XmlRootElement
public class PlayerGroovy {
    String value
}

参照: XmlAccessType

私を正しい方向に導いてくれた元の答えに感謝します。

于 2009-10-21T17:05:45.390 に答える
1

ソリューションは、抽象サブクラスでは機能しないようです。getMetaClassコンパイラがオーバーライド コードを生成しないためだと思います。この質問の手順を次のように模倣することになりました。

@XmlAccessorType(XmlAccessType.NONE)
package groovy.lang;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

はい、ちょっと変です。私の場合、次のようなコードがあります。

package pkg;
abstract class ScriptMomma extends groovy.lang.Script {
    // write some nice supporting code here
}

スクリプトを実行するには、次のものが必要です。

def config = new org.codehaus.groovy.control.CompilerConfiguration()
config.scriptBaseClass = 'pkg.ScriptMomma'
ScriptMomma mine = new GroovyShell(config).evaluate(scriptFile, 'TheOne')

私はより良い解決策を望んでいますが、今のところこれが私が持っているすべてです。

于 2014-01-17T05:48:22.440 に答える