元帅行为的差异 [英] Discrepancy in marshal behaviour

查看:83
本文介绍了元帅行为的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试MOXy 2.5.0 RC1。

I am testing MOXy 2.5.0 RC1.

我将以下内容编组为字符串:

I marshalled the following to a string:

  <c r="C3"  xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <v>20</v>
  </c>

它由 https://github.com/plutext/docx4j/blob/master/src/xlsx4j/java/org/ xlsx4j / sml / Cell.java

注意没有任何@XmlRootElement注释

Notice the absence of any @XmlRootElement annotation

随着参考实现,结果如预期的那样:

With the reference implementation, the result, as expected, is:

javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "org.xlsx4j.sml.Cell" as an element because it is missing an @XmlRootElement annotation]
        at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:317)
        at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243)
        at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96)
        at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:507)

使用MOXy,结果为:

With MOXy, the result is:

 <v>20</v>

这是一个已知问题吗?我没试过2.4.2 RC1。

Is this a known issue? I haven't tried 2.4.2 RC1.

谢谢..

推荐答案

这是 EclipseLink MOXy 和RI。我们已经在MOXy中打开了这个门,用于编组一个 OutputStream Writer 的用例元素已被写入。

That is a known difference between EclipseLink MOXy and the RI. We have left this door open in MOXy for the use case where you are marshalling into an OutputStream or Writer where the root element has already been written.

您是否指望抛出异常。如果没有根元素,则可以将对象包装在 JAXBElement 的实例中。

Are you counting on an exception being thrown. When there is no root element you can wrap the object in an instance of JAXBElement.

解决方法

您可以使用 JAXBIntrospector 来确定对象是否具有根元素。

You can use a JAXBIntrospector to determine if an object has a root element.

JAXBIntrospector introspector = jaxbContext.createJAXBIntrospector();
QName rootElement = introspector.getElementName(aPOJO);
if(null == rootElement) {
    // ...
} else {
    // ...
}

这篇关于元帅行为的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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