开发者

JAXB auto generated classes hierarchy

开发者 https://www.devze.com 2023-02-03 11:58 出处:网络
I\'m generating java code based on various WSDL\'s. We have a different WSDL for every new version of the WebService that we release, each with its o开发者_高级运维wn namespace.

I'm generating java code based on various WSDL's. We have a different WSDL for every new version of the WebService that we release, each with its o开发者_高级运维wn namespace.

The thing is that normally the changes are minimal from one release to another, but I want to keep classes divided by namespace.

Is there a way to configure JAXB so that the auto generated classes implement a single interface/extend a single class, so I can refer to either of them without changing my code?

Dummy example:

WebService method: listScripts(ResultSize size);

Auto generated classes:

  • com.test.ws1.ResultSize
  • com.test.ws2.ResultSize

Both classes are exactly the same. Is there a way to arrange them in a class hierarchy so my code is isolated from changes in version numbers? i.e. a com.test.ResultSize interface implemented by both classes?


XJC has an extension for this purpose

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
           xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
           jaxb:extensionBindingPrefixes="xjc"
           jaxb:version="2.0">

    <xs:annotation>
       <xs:appinfo>
          <jaxb:globalBindings>
           <xjc:superClass name="com.mycompany.xml.UserRootObject"/>
          </jaxb:globalBindings>
       </xs:appinfo>
    </xs:annotation>
.
.
.
</xs:schema>

For more information see:

  • http://jaxb.java.net/nonav/2.0.2/docs/vendorCustomizations.html

The schema annotations can also be supplied via an external bindings file. For an example see:

  • How do you customize how JAXB generates plural method names?


It turned out that I can use a plugin provided in the JAXB2 Basics package:

Inheritance plugin

With this plugin I can specify different super classes for my generated ones, although I couldn't make the auto generated enums to implement a given interface.

To use it in Maven it was a pain (I'm generating classes from a WSDL, not using JAXB directly), so I switched to an external Ant task as specified in this blog

0

精彩评论

暂无评论...
验证码 换一张
取 消