Java中的XSD命名空间问题

时间:2018-04-10 12:34:37

标签: java soap xsd pom.xml jaxb2-maven-plugin

我创建了2个带有不同名称空间的xsd,并尝试将Customer.XSD导入Main.XSD,如下所示:

Main.XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://COM.SERVICES.com"
targetNamespace="http://COM.SERVICES.com" xmlns:s2="WebServices.Cutomer" elementFormDefault="qualified">

 <xs:import namespace="WebServices.Cutomer" schemaLocation="Customer.xsd" />

    <xs:element name="CustRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="id" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="CustReSponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Customer" type="s2:Customer"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

Customer.XSD

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

   <xs:complexType name="Customer">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="id" type="xs:string"/>
            <xs:element name="age" type="xs:int"/>
            <xs:element name="country" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

</xs:schema>

以下不起作用:

<xs:import namespace="WebServices.Cutomer" schemaLocation="Customer.xsd" />

由于上面的导入标记在Pom.xml中出现错误,如下所示:

(org.codehaus.mojo:jaxb2-maven-plugin:2.3.1:xjc:xjc:generate-sources)

org.apache.maven.plugin.MojoExecutionException: 
    at org.codehaus.mojo.jaxb2.javageneration.AbstractJavaGeneratorMojo.performExecution(AbstractJavaGeneratorMojo.java:481)
    at org.codehaus.mojo.jaxb2.AbstractJaxbMojo.execute(AbstractJaxbMojo.java:257)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.eclipse.m2e.core.internal.embedder.MavenImpl.execute(MavenImpl.java:331)
    at org.eclipse.m2e.core.internal.embedder.MavenImpl$11.call(MavenImpl.java:1362)
    at org.eclipse.m2e.core.internal.embedder.MavenImpl$11.call(MavenImpl.java:1)
    at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:177)
    at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:112)
    at org.eclipse.m2e.core.internal.embedder.MavenImpl.execute(MavenImpl.java:1360)
    at org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant.build(MojoExecutionBuildParticipant.java:52)
    at org.eclipse.m2e.core.internal.builder.MavenBuilderImpl.build(MavenBuilderImpl.java:137)
    at org.eclipse.m2e.core.internal.builder.MavenBuilder$1.method(MavenBuilder.java:172)
    at org.eclipse.m2e.core.internal.builder.MavenBuilder$1.method(MavenBuilder.java:1)
    at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod$1$1.call(MavenBuilder.java:115)
    at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:177)
    at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:112)
    at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod$1.call(MavenBuilder.java:105)
    at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:177)
    at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:151)
    at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:99)
    at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod.execute(MavenBuilder.java:86)
    at org.eclipse.m2e.core.internal.builder.MavenBuilder.build(MavenBuilder.java:200)
    at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:735)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:206)
    at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:246)
    at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:301)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:304)
    at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:360)
    at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:383)
    at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:142)
    at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:232)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:56)
Caused by: org.codehaus.mojo.jaxb2.NoSchemasException
    at org.codehaus.mojo.jaxb2.javageneration.AbstractJavaGeneratorMojo.getXjcArguments(AbstractJavaGeneratorMojo.java:593)
    at org.codehaus.mojo.jaxb2.javageneration.AbstractJavaGeneratorMojo.performExecution(AbstractJavaGeneratorMojo.java:387)
    ... 33 more

的pom.xml:

 <plugin>
   <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
     <execution>
       <id>xjc</id>
       <goals>
         <goal>xjc</goal>
       </goals>
     </execution>
   </executions>
   <configuration>
        <schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory>
        <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
    </configuration>
</plugin>

因为我没有得到上面xsd的问题。请检查并提出建议。

1 个答案:

答案 0 :(得分:0)

xs:import声明(namespace="WebServices.Cutomer")中给出的命名空间必须与要导入的模式文档的目标命名空间匹配(targetNamespace="http://WebServices.Com"