我有2个服务XSD文件AService.xsd和BService.xsd,每个文件都有不同的targetNamespace.这两个都使用一个名为common.xsd的通用XSD.我使用JAXB 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>
<generatePackage>com.schema</generatePackage>
<generateDirectory>src/main/java</generateDirectory>
</configuration>
</execution>
Run Code Online (Sandbox Code Playgroud)
当我尝试运行这个时,我得到以下错误.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.
Run Code Online (Sandbox Code Playgroud)
如果我在2个不同的执行中运行2个服务xsds生成2个不同的包,我ValidationType在2个不同的包中获得相同的类.
有关如何使JAXB识别共享模式的任何想法?
你正面临一个所谓的"变色龙图式",这被认为是一种不好的做法.不幸的是,由于JAXB的性质,没有好的解决方案.JAXB注释将bean属性绑定到特定名称空间中的XML元素和属性(在模式编译时确定).因此,一旦编译了模式,就没有官方的好方法来更改属性绑定到的元素和属性的名称空间.
但是,这正是您想要通过"变色龙"模式实现的目标.从"common.xsd"派生的类应该以某种方式神奇地映射到命名空间A(如果在A类中使用)和命名空间B(如果在B类中使用).我可以想象这种魔力,但在现实生活中从未见过.
由于你基本上希望A/common和B/common是"相同的东西",解决它的方法之一是在两次执行中生成A和B(两者都有共同)并使公共类实现某个"共同" "界面.然后你的软件可以在同一个方面处理A/common和B/common,而不管这些实际上是来自不同包的类.
更新:
从评论我看到你没有变色龙模式,只是一个正常的导入.这很简单,只需分别编译common,A和B. 请参阅maven-jaxb2-plugin 的单独模式编译.
我按照这里描述的方式定制了包.因此,它们位于不同的命名空间中common.xsd,com.common.schema并且由它们共享,AService.xsd并且BService.xsd它们都在不同的包中.
在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>
Run Code Online (Sandbox Code Playgroud)