我如何让Groovy和JAXB一起玩 [英] How do I get Groovy and JAXB to play nice together

查看:139
本文介绍了我如何让Groovy和JAXB一起玩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让JAXB和我的groovy类一起工作,然而,它看起来不起作用,但java版本却行。这里是代码...

以下是方案:

如果2和3未注释,



如果1和4没有注释,我会得到:

  com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:
IllegalAnnotationExceptions
的计数groovy.lang.MetaClass是一个接口,JAXB无法处理接口。

如果1和5未注释,我会得到:

  javax.xml.bind.JAXBException:类org.oclc.presentations.simplejaxb.PlayerGroovy 
也不知道它的任何超类。

有什么想法?

Java: / p>

  import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement
public class Player {
}

Groovy:

$ p $ 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
$ b $ 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)
}
}


解决方案1和4是使用Groovy设置JAXB的正确方法。它不工作的原因是每个Groovy类都有一个metaClass属性。 JAXB正试图将其作为一个显然失败的JAXB属性公开。由于您不需要自己声明metaClass属性,因此无法注释它让JAXB忽略它。相反,你将XmlAccessType设置为NONE。此禁用的JAXB自动发现属性以公开为XML元素。这样做后,您需要显式声明任何您想要公开的字段。



示例:

  @XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class PlayerGroovy {
@XmlAttribute
字符串值
}


I am trying to get JAXB to work with a groovy class of mine, however, it appears it doesn't work but the java version does. Here is the code...

Here are the Scenarios:

If 2 and 3 are uncommented it works fine.

If 1 and 4 are uncommented I get:

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

If 1 and 5 are uncommented I get:

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

Any ideas?

Java:

    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
    public class Player {
    }

Groovy:

    import javax.xml.bind.annotation.XmlRootElement

    @XmlRootElement
    public class PlayerGroovy {
    }

Test:

    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)
        }
    }

解决方案

Uncommenting 1 and 4 is the correct way to set JAXB up with Groovy. The reason it is not working is that each Groovy Class has a metaClass property on it. JAXB is trying to expose this as a JAXB property which obviously fails. Since you don't declare the metaClass property yourself, it is not possible to annotate it to have JAXB ignore it. Instead you and set the XmlAccessType to NONE. This disable's JAXB's auto-discovery of properties to expose as XML elements. After you do that, you need to explicitly declare any fields you want exposed.

Example:

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

这篇关于我如何让Groovy和JAXB一起玩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆