简体   繁体   English

JAXB:为两个共享一个共同XSD的XSD生成类

[英]JAXB: Generating classes for two XSDs which share a common XSD

I have 2 service XSD files AService.xsd and BService.xsd each with different targetNamespace. 我有2个服务XSD文件AService.xsd和BService.xsd,每个文件都有不同的targetNamespace。 Both of these use a common XSD called common.xsd. 这两个都使用一个名为common.xsd的通用XSD。 I use the JAXB Maven plugin to generate classes. 我使用JAXB Maven插件生成类。 Here's how, 这是如何做,

<execution>
    <id>generate-package</id>
    <goals>
        <goal>generate</goal>
    </goals>
    <configuration>
        <extension>true</extension>
        <schemaIncludes>
            <include>schema/Aservice.xsd</include>
            <include>schema/Bservice.xsd</include>                             
        </schemaIncludes>
        <bindingIncludes>                                   
            <include>schema/*.xjb</include>
        </bindingIncludes>
        <generatePackage>com.schema</generatePackage>
        <generateDirectory>src/main/java</generateDirectory>
    </configuration>
</execution>

When i try to run this i get the following error. 当我尝试运行这个时,我得到以下错误。 ValidationType is defined in common.xsd ValidationType在common.xsd中定义

org.xml.sax.SAXParseException: A class/interface with the same name "com.schema.ValidationType" is already in use. Use a class customization to resolve this conflict.
..........
org.xml.sax.SAXParseException: (Relevant to above error) another "ValidationType" is generated from here.
......
com.sun.istack.SAXParseException2: Two declarations cause a collision in the ObjectFactory class.

If i run the 2 service xsds in 2 different executions generating into 2 different packages, i get the same ValidationType class in 2 different packages. 如果我在2个不同的执行中运行2个服务xsds生成2个不同的包,我在2个不同的包中获得相同的ValidationType类。

Any ideas on how to make JAXB recognize shared schemas? 有关如何使JAXB识别共享模式的任何想法?

You are facing a so-called "chameleon schema" which is considered to be a bad practice. 你正面临一个所谓的“变色龙图式”,这被认为是一种不好的做法。 Unfortunately, there is no good solution due to the nature of JAXB. 不幸的是,由于JAXB的性质,没有好的解决方案。 JAXB annotation bind bean properties to XML elements and attributes in specific namespaces (determined in the schema compile time). JAXB注释将bean属性绑定到特定名称空间中的XML元素和属性(在模式编译时确定)。 So once your schema is compiled, there is no official good way to change namespaces of elements and attributes your properties are bound to. 因此,一旦编译了模式,就没有官方的好方法来更改属性绑定到的元素和属性的名称空间。

However, this is exactly what you want to achieve with "chameleon" schemas. 但是,这正是您想要通过“变色龙”模式实现的目标。 Classes derived from "common.xsd" should somehow magically map to namespace A if used in A classes and to namespace B if used in B classes. 从“common.xsd”派生的类应该以某种方式神奇地映射到命名空间A(如果在A类中使用)和命名空间B(如果在B类中使用)。 I can imagine this magic, but never seen in in real life. 我可以想象这种魔力,但在现实生活中从未见过。

Since you essentially want A/common and B/common to be the "same thing", one of the ways to resolve it is to generate A and B (both with common) in two executions and to make common classes implement a certain "common" interface. 由于你基本上希望A / common和B / common是“相同的东西”,解决它的方法之一是在两次执行中生成A和B(两者都有共同)并使公共类实现某个“共同” “界面。 Then your software could process A/common and B/common in the same faschion regardless of the fact that these are actually classes from the different packages. 然后你的软件可以在同一个方面处理A / common和B / common,而不管这些实际上是来自不同包的类。

UPDATE: 更新:

From the comment I see that you don't have a chameleon schema, but just a normal importing. 从评论我看到你没有变色龙模式,只是一个正常的导入。 It is easy then, just compile common, A and B separately. 这很简单,只需分别编译common,A和B. See the Separate schema compilation for maven-jaxb2-plugin. 请参阅maven-jaxb2-plugin的单独模式编译

I customized the packages as described here . 我按照这里描述的方式定制了包 So common.xsd goes in com.common.schema and is shared by AService.xsd and BService.xsd which are both in different packages themselves, since they are in different namespaces. 所以common.xsd位于com.common.schema ,由AService.xsdBService.xsd共享,它们本身都在不同的包中,因为它们位于不同的命名空间中。

The generatePackage is removed from the maven configuration and looks like this, generatePackage将从maven配置中删除,如下所示,

<execution>
    <id>generate-package</id>
    <goals>
        <goal>generate</goal>
    </goals>
    <configuration>
        <extension>true</extension>
        <schemaIncludes>
            <include>schema/Aservice.xsd</include>
            <include>schema/Bservice.xsd</include>                            
        </schemaIncludes>
        <bindingIncludes>
            <include>schema/*.xjb</include>
        </bindingIncludes>                                
        <generateDirectory>src/main/java</generateDirectory>
    </configuration>
</execution>

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

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