如何使用类自定义来解决文件生成的冲突

2022-09-01 14:51:07

我正在尝试使用Maven生成JAXB文件以供Spring框架使用,但Maven显示以下错误:

我知道它无法生成具有名称的文件,但我不确定如何解决问题。到目前为止,我访问了以下链接。123

org.xml.sax.SAXParseException; systemId: http://www5v80.elsyarres.net/service.asmx?wsdl; lineNumber: 5; columnNumber: 39; A class/interface with the same name "hello.wsdl.SearchFlights" is already in use. Use a class customization to resolve this conflict.
....
org.xml.sax.SAXParseException; systemId: http://www5v80.elsyarres.net/service.asmx?wsdl; lineNumber: 12; columnNumber: 43; (Relevant to above error) another "SearchFlights" is generated from here.
....
org.xml.sax.SAXParseException; systemId: http://www5v80.elsyarres.net/service.asmx?wsdl; lineNumber: 371; columnNumber: 42; A class/interface with the same name "hello.wsdl.GetFlightDetails" is already in use. Use a class customization to resolve this conflict.
....

Maven 插件

    <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.12.3</version>
        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <schemaLanguage>WSDL</schemaLanguage>
            <generatePackage>hello.wsdl</generatePackage>
            <schemas>
                <schema>
                    <url>http://www5v80.elsyarres.net/service.asmx?wsdl</url>
                </schema>
            </schemas>
        </configuration>
    </plugin>

我将以下文件添加到包中,但它没有帮助。package-info.javahello.wsdl

@XmlSchema( 
    namespace = "ElsyArres.API",
    elementFormDefault = XmlNsForm.QUALIFIED) 
package hello.wsdl;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

答案 1

您面临的错误消息基本上指出,wsdl 部分中的某些名称被使用了两次。在您的例子中,所有标签都与其相应的类型(定义为 )具有相同的名称。types<element><complexType>

例:

  <s:element name="SearchFlights">
    <s:complexType>
      <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="SoapMessage" type="tns:SearchFlights" />
      </s:sequence>
    </s:complexType>
  </s:element>

  <s:complexType name="SearchFlights">
    <s:complexContent mixed="false">
      <s:extension base="tns:SoapMessageBase">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="1" name="Request" type="tns:SearchFlightsRequest" />
          <s:element minOccurs="0" maxOccurs="1" name="Response" type="tns:SearchFlightsResponse" />
        </s:sequence>
      </s:extension>
    </s:complexContent>
  </s:complexType>

这种情况并不常见。

基本上有两种方法可以解决这些问题:

使用自动名称解析

 <plugin>
     <groupId>org.jvnet.jaxb2.maven2</groupId>
     <artifactId>maven-jaxb2-plugin</artifactId>
     <version>0.13.1</version>
     <executions>
         <execution>
             <goals>
                 <goal>generate</goal>
             </goals>
         </execution>
     </executions>
     <configuration>

         <args>
             <arg>-XautoNameResolution</arg>
         </args>

         <schemaLanguage>WSDL</schemaLanguage>
         <generatePackage>hello.wsdl</generatePackage>
         <schemas>
             <schema>
                 <url>http://www5v80.elsyarres.net/service.asmx?wsdl</url>
             </schema>
          </schemas>
      </configuration>
  </plugin>

该插件将通过在每个碰撞名称上附加数字来解决所有命名冲突。在上面提到的 SearchFlights 案例中,这将导致生成 SearchFlightsSearchFlights2

更好的方法是使用绑定文件提前解决所有名称冲突。绑定文件主要包含表达式和转换规则。附加到每个声明名称的绑定文件如下所示:XPATH

<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings wsdlLocation="http://www5v80.elsyarres.net/service.asmx?wsdl"
            xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
            xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" version="2.1"
            xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='ElsyArres.API']">
        <jaxb:schemaBindings>
            <jaxb:nameXmlTransform>
                <jaxb:elementName suffix="Elem"/>
            </jaxb:nameXmlTransform>
        </jaxb:schemaBindings>
    </jaxws:bindings>
</jaxws:bindings>

还有其他选项可用于类似后缀和附加到其他类型的 xml 元素(如类型)之前。jaxb:nameXmlTransform

可悲的是,我无法使用(但我确定有一个工作配置)来处理这个绑定文件org.jvnet.jaxb2.maven2:maven-jaxb2-plugin

尽管如此,它仍然适用于org.codehaus.mojo:jaxws-maven-plugin和以下配置。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>2.4.1</version>
    <executions>
        <execution>
            <goals>
                <goal>wsimport</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <bindingFiles>
         <bindingFile>${basedir}/src/main/resources/bindings.xjb</bindingFile>
        </bindingFiles>
        <wsdlUrls>
            <wsdlUrl>http://www5v80.elsyarres.net/service.asmx?wsdl</wsdlUrl>
        </wsdlUrls>
        <vmArgs>
            <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
        </vmArgs>
    </configuration>
</plugin>

答案 2

如果自动名称解析修复

 <args>
     <arg>-XautoNameResolution</arg>
 </args>

不起作用,请尝试:

 <args>
     <arg>-B-XautoNameResolution</arg>
 </args>

推荐