元素在WSDL中具有相同的名称和不同的类型

时间:2018-04-23 14:00:54

标签: java jaxb jax-ws wsdl2java

我获得了WSDL和模式文件。我正在使用这些文件在Java中创建存根。但是我在生成错误时遇到错误(如下)。我知道问题是什么我只是不知道如何解决它。我明白问题是有时PayLoadLength有时是字符串或整数

<xs:element name="PayloadLength" type="xs:string" minOccurs="0"/>

<xs:element name="PayloadLength" maxOccurs="1" minOccurs="0" type="xs:int"/>

模式文件(文件的一部分,你可以看到PayloadLength,它是一个int或字符串:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>-->
<xs:schema version="1.0" targetNamespace="xxxxxx" xmlns:tns="xxxxxxxxxx" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="ABCBatchResultsAckSubmission" type="tns:ABCBatchResultsAckSubmission"/>

  <xs:element name="ABCBatchResultsAckSubmissionResponse" type="tns:ABCBatchResultsAckSubmissionResponse"/>

  <xs:element name="ABCBatchResultsRetrievalRequest" type="tns:ABCBatchResultsRetrievalRequest"/>

  <xs:element name="ABCBatchResultsRetrievalRequestResponse" type="tns:ABCBatchResultsRetrievalRequestResponse"/>

  <xs:element name="ABCBatchSubmission" type="tns:ABCBatchSubmission"/>

  <xs:element name="ABCBatchSubmissionAckRetrievalRequest" type="tns:ABCBatchSubmissionAckRetrievalRequest"/>

  <xs:element name="ABCBatchSubmissionAckRetrievalRequestResponse" type="tns:ABCBatchSubmissionAckRetrievalRequestResponse"/>

  <xs:element name="ABCBatchSubmissionResponse" type="tns:ABCBatchSubmissionResponse"/>

  <xs:complexType name="ABCBatchSubmissionAckRetrievalRequest">
    <xs:sequence>

        <xs:element name="PayloadLength" type="xs:string" minOccurs="0"/>

    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ABCBatchSubmissionAckRetrievalRequestResponse">
    <xs:sequence>

    <xs:element name="PayloadLength" maxOccurs="1" minOccurs="0" type="xs:int"/>

    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ABCBatchSubmission">
    <xs:sequence>

      <xs:element name="PayloadLength" type="xs:string" minOccurs="0"/>

    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ABCBatchSubmissionResponse">
    <xs:sequence>

        <xs:element name="PayloadLength" maxOccurs="1" minOccurs="0" type="xs:int"/>


    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ABCBatchResultsRetrievalRequest">
    <xs:sequence>

      <xs:element name="PayloadLength" type="xs:string" minOccurs="0"/>

    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ABCBatchResultsRetrievalRequestResponse">
    <xs:sequence>

        <xs:element name="PayloadLength" maxOccurs="1" minOccurs="0" type="xs:int"/>

    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ABCBatchResultsAckSubmission">
    <xs:sequence>

      <xs:element name="PayloadLength" type="xs:string" minOccurs="0"/>

    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ABCBatchResultsAckSubmissionResponse">
    <xs:sequence>

        <xs:element name="PayloadLength" maxOccurs="1" minOccurs="0" type="xs:int"/>

    </xs:sequence>
  </xs:complexType>
</xs:schema>

所以我知道我需要创建一个JAXB Binding文件来解决问题。我很确定这是错的(下图)。但我认为我需要定位PayLoadLength并将其名称更改为其他名称。 是吗?

的pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>${basedir}/src/main/resources/wsdl/CORETransactionService.wsdl</wsdl>
                                    <wsdlLocation>classpath:wsdl/CORETransactionService.wsdl</wsdlLocation>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

错误:

Execution generate-sources of goal org.apache.cxf:cxf-codegen-plugin:3.2.1:wsdl2java failed: Element PayloadLength has the same name with different types[{http://www.w3.org/2001/XMLSchema}string -- {http://
 www.w3.org/2001/XMLSchema}int]. This will cause a collision in wrapper style as per the spec. (org.apache.cxf:cxf-codegen-plugin:3.2.1:wsdl2java:generate-sources:generate-sources) 
 org.apache.maven.plugin.PluginExecutionException: Execution generate-sources of goal org.apache.cxf:cxf-codegen-plugin:3.2.1:wsdl2java failed: Element PayloadLength has the same name with different types[{http://
 www.w3.org/2001/XMLSchema}string -- {http://www.w3.org/2001/XMLSchema}int]. This will cause a collision in wrapper style as per the spec. at 
 org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:145) 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.apache.cxf.tools.common.ToolException: Element PayloadLength has the same name with different types[{http://www.w3.org/2001/XMLSchema}string -- {http://www.w3.org/2001/XMLSchema}int]. This 
 will cause a collision in wrapper style as per the spec. at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.validate(WSDLToJavaContainer.java:735) at 
 org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.processWsdl(WSDLToJavaContainer.java:276) at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.execute(WSDLToJavaContainer.java:164) at 
 org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.execute(WSDLToJavaContainer.java:412) at org.apache.cxf.tools.common.toolspec.ToolRunner.runTool(ToolRunner.java:105) at 
 org.apache.cxf.tools.wsdlto.WSDLToJava.run(WSDLToJava.java:113) at org.apache.cxf.tools.wsdlto.WSDLToJava.run(WSDLToJava.java:86) at 
 org.apache.cxf.maven_plugin.wsdl2java.WSDL2JavaMojo.generate(WSDL2JavaMojo.java:414) at org.apache.cxf.maven_plugin.AbstractCodegenMoho.execute(AbstractCodegenMoho.java:279) at 
 org.apache.cxf.maven_plugin.wsdl2java.WSDL2JavaMojo.execute(WSDL2JavaMojo.java:511) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134) ... 31 more

1 个答案:

答案 0 :(得分:1)

经过相当广泛的分析后,问题似乎出现在所谓的“包装风格”中。引用WSDL To Java

  

注意:JAX-WS 2.1规范中定义的“包装样式”和“非包装样式”的含义可能违反直觉。包装样式表示请求消息中的每个数据元素都有自己的Java参数,而非包装样式表示包含所有数据元素的单个Java对象充当Web服务方法调用的唯一参数。

所以基本上问题是如果请求中有一些子元素和具有相同名称的响应类型,它们实际上将映射到一个参数。如果它们有不同的类型,则会导致冲突,CXF会拒绝它们。

解决此问题的一种方法是禁用包装样式。您可以使用binding.xml文件执行此操作,例如:

<jaxws:bindings node="wsdl:definitions" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
    <jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
</jaxws:bindings>

将其包含在cxf-codegen-plugin

的配置中
        <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-codegen-plugin</artifactId>
            <version>3.2.1</version>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <wsdlOptions>
                            <wsdlOption>
                                <wsdl>${basedir}/src/main/resources/wsdl/My.wsdl</wsdl>
                                <wsdlLocation>classpath:wsdl/My.wsdl</wsdlLocation>
                                <bindingFiles>
                                    <bindingFile>${basedir}/src/main/resources/bindings.xml</bindingFile>
                                </bindingFiles>
                            </wsdlOption>
                        </wsdlOptions>
                    </configuration>
                    <goals>
                        <goal>wsdl2java</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

还有可能另一种方式,至少是错误消息,至少错误消息表明应该可以“使用JAXWS / JAXB绑定自定义来重命名参数”。不幸的是,我无法找到正确的绑定语法。