【问题标题】:JAXB unmarshal callbacks not called with the Binder implementation of EclipseLink MoxyEclipseLink Moxy 的 Binder 实现未调用 JAXB 解组回调
【发布时间】:2013-11-08 08:21:55
【问题描述】:

我对 javax.xml.bind.Binder implementation of EclipseLink Moxy 有疑问:当我从 DOM 文档实例解组对象时,永远不会调用绑定类中注释的 unmarshal callback methods(在我的情况下为 afterUnmarshal)。

问题似乎出在 Binder 使用的 org.eclipse.persistence.oxm.XMLUnmarshaller 实例中:负责调用回调方法的 unmarshalListener 始终为 null。这段代码演示了这个问题:

//Instantiate a JAXBContext
JAXBContext context = JAXBContext.newInstance(MyObject.class);

//Create a "standard" unmarshaller
JAXBUnmarshaller standardUnmarshaller = (JAXBUnmarshaller) context.createUnmarshaller();

//assertion is ok
assert standardUnmarshaller.getXMLUnmarshaller().getUnmarshalListener() != null;

//Create a Binder
XMLBinder xmlBinder = ((JAXBBinder) context.createBinder()).getXMLBinder();

//Use reflection tricks to get the unmarshaller (fest-reflect is used for conciseness)
XMLUnmarshaller binderUnmarshaller = Reflection.field("unmarshaller").ofType(XMLUnmarshaller.class).in(xmlBinder).get();

//assertion error here
assert binderUnmarshaller.getUnmarshalListener() != null;

这不容易解决,因为没有 javax.xml.bind.Unmarshaller 实例就无法实例化 org.eclipse.persistence.jaxb.JAXBUnmarshalListener,该实例在 Binder 上下文中不存在。 Moxy 中没有其他可用的 XMLUnmarshalListener 实现来替换 JAXBUnmarshalListener。

我想出了这个解决方法,它包括从“标准”JAXB 解组器重用 JAXBUnmarshalListener 实例。 :

//Instantiate a JAXBContext
JAXBContext context = JAXBContext.newInstance(MyObject.class);

//Create a Binder
JAXBBinder binder = (JAXBBinder) context.createBinder();

//Create an standard unmarshaller to reuse its unmarshalListener
JAXBUnmarshaller standardUnmarshaller = (JAXBUnmarshaller) context.createUnmarshaller();

//Use reflection for setting the binder's unmarshallerListener (fest-reflect again)
XMLUnmarshaller unmarshaller = Reflection.field("unmarshaller").ofType(XMLUnmarshaller.class).in(binder.getXMLBinder()).get();
unmarshaller.setUnmarshalListener(standardUnmarshaller.getXMLUnmarshaller().getUnmarshalListener());

//my unmarshal callbacks are called now
MyObject myObject = binder.unmarshal(domDocument);

我想问题与封送处理方法相同,但我没有调查这种情况,因为我不使用封送回调。

我测试了javax.xml.bind.Binder的JAXB RI实现,回调方法调用正确。不过,我不能选择使用 RI,因为它不会保留原始 DOM 文档中的注释节点。

我是否错过了 Moxy 的配置中的某些内容,或者这是实现中的错误?如果是错误,是否有比我使用的更简单的解决方法?

【问题讨论】:

    标签: java xml jaxb eclipselink moxy


    【解决方案1】:

    我已经能够确认您所看到的问题。您可以使用以下链接跟踪我们在此问题上的进展:

    【讨论】:

      猜你喜欢
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      相关资源
      最近更新 更多