JAXB 需要一个公共的无参数构造函数来做什么?

2022-01-19 00:00:00 xml java jaxb

在编组期间,JAXB 需要一个公共的无参数构造函数来做什么?

What does JAXB need a public no-arg constructor for, during marshalling?

 Marshaller msh = ctx.createMarshaller();
 msh.marshal(object, System.out);

我传递的是一个对象,而不是一个类.为什么 JAXB 需要构造函数?建造什么?

I'm passing an object, not a class. Why does JAXB need a constructor? To construct what?

推荐答案

在封送操作期间,JAXB 实现不应该需要无参数构造函数.JAXB 确实需要一个用于解组.通常,在创建 JAXBContext 时,缺少无参数构造函数会导致错误.您使用的 JAXB 实现可能会延迟初始化,直到执行实际操作.

A JAXB implementation should not need a no-arg constructor during a marshal operation. JAXB does require one for unmarshalling. Normally the absence of a no-arg constructor causes an error when the JAXBContext is created. The JAXB implementation you are using may be delaying initialization until an actual operation is performed.

一般来说,我们应该在 JAXB 的未来版本中考虑对多参数构造函数的支持.在 EclipseLink 的 JAXB (MOXy) 实现中,我们对此功能开放了一个增强请求(请随意添加相关的详情):

In general support for multi-arg constructors is something we should consider in a future version of JAXB. In the EclipseLink implementation of JAXB (MOXy) we have an enhancement request open for this functionality (feel free to add relevant details):

  • https://bugs.eclipse.org/328951.

在当前版本的 JAXB 中,您可以使用 XmlAdapter 来支持此用例:

In the current version of JAXB you could use an XmlAdapter to support this use case:

  • http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html

相关文章