简体   繁体   English

如何自定义jaxb生成?

[英]How do I customise jaxb generation?

I have a series of xml messages, all with their own schemas and namespaces. 我有一系列xml消息,都有自己的模式和命名空间。 The messages are currently marshalled using JAXB (we still live in a Java 1.4 environment) and we have a large amount of legacy code using this JAXB code so any solution needs to be minimally intrusive. 消息当前使用JAXB编组(我们仍然生活在Java 1.4环境中),并且我们使用此JAXB代码有大量遗留代码,因此任何解决方案都需要极少干扰。

My problem is that while each of the messages has a set of common header tags the namespace covers the entire message and therefore makes the header for each message unique as well. 我的问题是,虽然每个消息都有一组公共标头标签,但命名空间覆盖整个消息,因此每个消息的标头也是唯一的。 As a result the "common" header sections are loaded into the namespace bound versions using a common class that is implemented using very ugly proxy classes and dynamic reflection code. 因此,使用一个使用非常丑陋的代理类和动态反射代码实现的公共类,将“公共”头部分加载到命名空间绑定版本中。 This common class has been identified as the source of some performance issues. 这个常见类已被确定为一些性能问题的根源。

Ideally I want to implement a replacement using the following: 理想情况下,我想使用以下方法实现替换:

  • Use Maven2 to replace current manual jaxb build process. 使用Maven2替换当前手动jaxb构建过程。
  • Generate once-off JAXB classes for "common" headers to be reused in each message. 为“公共”标头生成一次性JAXB类,以便在每条消息中重用。
  • Convert current dynamic/proxy classes to use the above concrete header classes. 转换当前的动态/代理类以使用上面的具体标头类。
  • Generate JAXB classes for unqiue section of each message. 为每条消息的unqiue部分生成JAXB类。

Unfortunately, I don't have control of the message structure otherwise I would look at creating a seperate "header" namespace. 不幸的是,我无法控制消息结构,否则我会考虑创建一个单独的“头”命名空间。 I thought of running an XSLT transform to "rename" the header namespace after marshalling and prior to unmarshalling but I would prefer to avoid the extra load if possible, even if it means a more complex build. 我想在编组之后和解组之前运行一个XSLT转换来“重命名”头名称空间,但是如果可能的话我宁愿避免额外的负载,即使它意味着更复杂的构建。

Is what I want to do feasible or have I missed something fundamental? 我想做的是可行的还是我错过了一些基本的东西? Is there any hints as to how to implement? 有没有关于如何实施的提示? Plugin versions etc? 插件版本等?

Addendum 1 : Binding using javaType would do the job but it appears that doesn't work for complex types. 附录1 :使用javaType进行绑定可以完成这项工作,但似乎不适用于复杂类型。

Addendum 2 : Binding using class almost does it as well but I would want it to specify a specific class and package so I could ignore the generated duplicates. 附录2 :使用绑定几乎也是这样但我希望它指定一个特定的类包,所以我可以忽略生成的重复项。

JAXB 2.x has a @XmlJavaTypeAdapter annotation which may be the solution to your problem (see this blog by Kohsuke Kawaguchi). JAXB 2.x有一个@XmlJavaTypeAdapter注释,可能是您问题的解决方案(请参阅Kohsuke Kawaguchi的博客 )。

You can map your common header class to the generated namespace-specific header classes with an implementation of XmlAdapter<XMLHeaderFromNamespaceX, CommonHeader> and use the adapter with XmlJavaTypeAdapter . 您可以使用XmlAdapter<XMLHeaderFromNamespaceX, CommonHeader>的实现将公共头类映射到生成的特定于命名空间的头类XmlAdapter<XMLHeaderFromNamespaceX, CommonHeader>适配器与XmlJavaTypeAdapter一起XmlJavaTypeAdapter

However, as a downside you would need an adapter for each of your particular namespaces. 但是,作为缺点,您需要为每个特定名称空间使用适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM