JAXB通过maven与几个具有空命名空间的模式

时间:2014-08-05 16:34:45

标签: java maven jaxb

我的目标是从几个xsd架构生成Java类。

我有四个XSD文件,它们都有一个空命名空间,我无法更改它们:

  • SimpleTypeDefinitions(所有其他人引用的misc类型)
  • 标题(由以下两个xsds引用)
  • SingleRequest
  • BatchRequest

SingleRequest和BatchRequest非常相似,它们都包含具有相同名称的元素。有一个带有单个XMLAny元素的通用Web服务。这可以(当前)包含SingleRequest或BatchRequest。这种设计不在我的掌控之中,绝对不能改变。

我想从xsds生成我的Java类,它对SingleRequest工作正常。我必须在单个webservice-method中包含带有XMLSeeAlso注释的生成类。现在我必须添加BatchRequest,我遇到了麻烦,因为它们存在冲突。

我首选的解决方案是将所有内容放在一个项目中(和一个pom.xml),以及来自不同包中生成的不同xsds的java类。在这一点上,我对任何解决方案都很满意。

我尝试了很多我在网上找到的不同解决方案(其中有几个在SO上),但是没有一个解决方案,据我所知,在每个解决方案中,XSD都位于不同的命名空间中。其中我试过:

  • 为每个xsd文件执行jaxb2-maven-plugin的几次
  • 剧集有多种方式(直接在maven-plugin执行中,在执行中使用命令行参数,引用生成的.episode文件)
  • 在jaxb绑定文件中重命名冲突的类名,我仍然得到" ...已定义"来自maven的错误。

我使用JAXB 2.2.7和maven-jaxb2-plugin 0.9.0。

有什么想法吗?

EDIT1 2014-06-08: 我创建了示例XSD文件来重现我的问题,我还包括一个示例pom。 使用包含的pom,Java类在不同的包中生成,但每个包都有一个类" Header.java"。这会在运行时导致以下错误(当我尝试调用web服务时):

  

javax.xml.ws.WebServiceException:无法创建JAXBContext导致   by:java.security.PrivilegedActionException:   com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:5   IllegalAnnotationExceptions的计数两个类具有相同的XML   输入姓名" {myService} myElement"。使用@ XmlType.name和   @ XmlType.namespace为它们分配不同的名称。

Header.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

  <xs:complexType name="HeaderType">
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" name="TimeStamp" type="xs:dateTime" nillable="false"/>
      <xs:element minOccurs="1" maxOccurs="1" name="TimeOut" type="xs:long" default="0" nillable="true"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

SingleRequest.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

<xs:include schemaLocation="Header.xsd" />
<xs:element name="SingleRequest">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="Header" type="HeaderType"/>
            <xs:element minOccurs="1" maxOccurs="1" name="Body">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="1" maxOccurs="1" name="RequestContent" type="RequestContentType"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="RequestContentType">
    <xs:sequence>
        <xs:element minOccurs="1" maxOccurs="1" nillable="false" name="RequestContentId" type="xs:string" />
    </xs:sequence>
</xs:complexType>

</xs:schema>

BatchRequest.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

<xs:include schemaLocation="Header.xsd" />
<xs:element name="BatchRequest">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="Header" type="HeaderType"/>
            <xs:element minOccurs="1" maxOccurs="1" name="Body">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="1" maxOccurs="500" name="RequestContent" type="RequestContentType"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="RequestContentType">
    <xs:sequence>
        <xs:element minOccurs="1" maxOccurs="1" nillable="false" name="RequestContentId" type="xs:string" />
        <xs:element minOccurs="1" maxOccurs="1" nillable="false" name="batchSpecificContent" type="xs:string" />
    </xs:sequence>
</xs:complexType>

</xs:schema>

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>JAXBProblem</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>.apt_generated</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${compiler.source.version}</source>
                    <target>${compiler.target.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/endorsed</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax.xml.bind</groupId>
                                    <artifactId>jaxb-api</artifactId>
                                    <version>2.2.7</version>
                                    <type>jar</type>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>javax.xml.ws</groupId>
                                    <artifactId>jaxws-api</artifactId>
                                    <version>2.2.9</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.9.0</version>
                <executions >
                    <execution>
                        <id>Header</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <generateDirectory>src/generated-sources/java</generateDirectory>
                            <generatePackage>com.example.header</generatePackage>
                            <schemaDirectory>src/main/resources</schemaDirectory>
                            <schemaIncludes>
                                <include>Header.xsd</include>
                            </schemaIncludes>
                            <bindingDirectory>src/main/resources</bindingDirectory>
                            <bindingIncludes>
                                <include>bindingsr.xjb</include>
                            </bindingIncludes>
                            <episode>true</episode>
                            <episodeFile>${basedir}/src/main/resources/header.episode</episodeFile>
                            <forceRegenerate>true</forceRegenerate>
                        </configuration>
                    </execution>
                    <execution>
                        <id>SingleRequest</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <extension>true</extension>
                            <generateDirectory>src/generated-sources/java</generateDirectory>
                            <generatePackage>com.example.singlerequest</generatePackage>
                            <schemaDirectory>src/main/resources</schemaDirectory>
                            <schemaIncludes>
                                <include>SingleRequest.xsd</include>
                            </schemaIncludes>
                            <bindingDirectory>src/main/resources</bindingDirectory>
                            <bindingIncludes>
                                <include>bindings.xjb</include>
                            </bindingIncludes>
                            <episode>false</episode>
                            <forceRegenerate>true</forceRegenerate>
                        </configuration>
                    </execution>
                    <execution>
                        <id>BatchRequest</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <extension>true</extension>
                            <generateDirectory>src/generated-sources/java</generateDirectory>
                            <generatePackage>com.example.batchrequest</generatePackage>
                            <schemaDirectory>src/main/resources</schemaDirectory>
                            <schemaIncludes>
                                <include>BatchRequest.xsd</include>
                            </schemaIncludes>
                            <bindingDirectory>src/main/resources</bindingDirectory>
                            <bindingIncludes>
                                <include>bindings.xjb</include>
                            </bindingIncludes>
                            <episode>false</episode>
                            <forceRegenerate>true</forceRegenerate>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>src/generated-sources/java</directory>
                            <includes>
                                <include>**/*</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

我有一个解决方案,但你的xsd并不好。

BatchRequest.xsd中的RequestContentType和SingleRequest.xsd中的RequestContentType是相同的类型。

如果你改变这种方式,一切正常......

Header.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

  <xs:complexType name="HeaderType">
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" name="TimeStamp" type="xs:dateTime" nillable="false"/>
      <xs:element minOccurs="1" maxOccurs="1" name="TimeOut" type="xs:long" default="0" nillable="true"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

BatchRequest.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

<xs:element name="BatchRequest">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="Header" type="HeaderType"/>
            <xs:element minOccurs="1" maxOccurs="1" name="Body">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="1" maxOccurs="500" name="RequestContent" type="RequestContentType"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="RequestContentType">
    <xs:sequence>
        <xs:element minOccurs="1" maxOccurs="1" nillable="false" name="RequestContentId" type="xs:string" />
        <xs:element minOccurs="1" maxOccurs="1" nillable="false" name="batchSpecificContent" type="xs:string" />
    </xs:sequence>
</xs:complexType>

</xs:schema>

SingleRequest.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

    <xs:element name="SingleRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="1" maxOccurs="1" name="Header"
                    type="HeaderType" />
                <xs:element minOccurs="1" maxOccurs="1" name="Body">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element minOccurs="1" maxOccurs="1" name="RequestContent"
                                type="RequestContentType" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

Context.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

<xs:include schemaLocation="Header.xsd" />
<xs:include schemaLocation="BatchRequest.xsd" />
<xs:include schemaLocation="SingleRequest.xsd" />

</xs:schema>

maven插件

<plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.8.1</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <arg>-Xannotate</arg>
                        <arg>-nv</arg>
                    </args>
                    <extension>true</extension>
                    <schemaDirectory>src/main/resources/schema/integration</schemaDirectory>
                    <schemaIncludes>
                        <schemaInclude>Context.xsd</schemaInclude>
                    </schemaIncludes>
                    <bindingDirectory>src/main/resources/schema/integration</bindingDirectory>
                    <catalog>src/main/resources/schema/integration/acme.cat</catalog>
                    <catalogResolver>org.jvnet.jaxb2.maven2.resolver.tools.ClasspathCatalogResolver</catalogResolver>
                    <debug>true</debug>
                    <verbose>true</verbose>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.6.0</version>
                        </plugin>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics-annotate</artifactId>
                            <version>0.6.0</version>
                        </plugin>
                    </plugins>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/generated-sources/xjc</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

我希望我已经为您提供了有关您问题的所有答案。