java - 如何使用带有名称冲突的 maven-jaxb2-plugin 映射 wsdl

标签 java binding maven-jaxb2-plugin

我在将 Web 服务映射到 Java POJO 时遇到问题,因为它包含两个元素:

  • 标签1
  • label_1

当插件将其转换为 java POJO 时,名称中的下划线将被删除,导致生成的类中出现重复字段。

[ERROR] Error while generating code.Location [ file:/C:/PrivateWS/test-project/sources/target/wsdl/test.wsdl{12,94}]. com.sun.istack.SAXParseException2; systemId: file:/C:/PrivateWS/test-project/sources/target/wsdl/test.wsdl; lineNumber: 12; columnNumber: 94; Two declarations cause a collision in the ObjectFactory class.

我尝试使用外部绑定(bind)文件将其中一个元素重命名为其他名称,但失败了。 我查看了很多不同的论坛,但找不到任何似乎有效的解决方案。据我所知,该属性未被识别,这会导致以下错误。

[ERROR] Error while parsing schema(s).Location [ file:/C:/PrivateWS/test-project/sources/target/classes/bindings.xjb{9,46}]. com.sun.istack.SAXParseException2; systemId: file:/C:/PrivateWS/test-project/sources/target/classes/bindings.xjb; lineNumber: 9; columnNumber: 46; compiler was unable to honor this property customization. It is attached to a wrong place, or its inconsistent with other bindings.

为了使字段重命名有效,我做错了什么?

更改 wsdl 不是一个选项,因为它是第三方 wsdl。

pom.xml

<?xml version="1.0"?><project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
     xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<modelVersion>4.0.0</modelVersion>

<groupId>com.testproject</groupId>
<artifactId>testproject</artifactId>
<version>0.0.0-SNAPSHOT</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-binding-file</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/src/main/resources</directory>
                                <includes>
                                    <include>bindings.xjb</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
                <execution>
                    <id>copy-wsdl-file</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/wsdl</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/src/main/resources/wsdl</directory>
                                <includes>
                                    <include>test.wsdl</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.13.1</version>
            <executions>
                <execution>
                    <id>TestProject</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <generatePackage>com.testproject</generatePackage>
                        <schemaIncludes>
                            <schemaInclude>wsdl/test.wsdl</schemaInclude>
                        </schemaIncludes>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <bindingDirectory>${project.build.outputDirectory}</bindingDirectory>
                <bindingIncludes>
                    <include>bindings.xjb</include>
                </bindingIncludes>
                <forceRegenerate>true</forceRegenerate>
                <schemaDirectory>${project.build.directory}</schemaDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

测试.wsdl

<definitions name="HelloService"
         targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl"
         xmlns="http://schemas.xmlsoap.org/wsdl/"
         xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
         xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:schema xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl" elementFormDefault="unqualified" targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl" version="1.0">
    <xsd:element name="requestType" type="tns:requestType"/>

    <xsd:complexType final="extension restriction" name="requestType">
        <xsd:sequence>
            <xsd:element form="qualified" minOccurs="0" name="label1" type="xsd:string"/>
            <xsd:element form="qualified" minOccurs="0" name="label_1" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

<message name="SayHelloRequest">
    <part element="tns:requestType" name="parameters"/>
</message>

<message name="SayHelloResponse">
    <part name="greeting" type="xsd:string"/>
</message>

<portType name="Hello_PortType">
    <operation name="sayHello">
        <input message="tns:SayHelloRequest"/>
        <output message="tns:SayHelloResponse"/>
    </operation>
</portType>

<binding name="Hello_Binding" type="tns:Hello_PortType">
    <soap:binding style="rpc"
                  transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="sayHello">
        <soap:operation soapAction="sayHello"/>
        <input>
            <soap:body
                    encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                    namespace="urn:examples:helloservice"
                    use="encoded"/>
        </input>

        <output>
            <soap:body
                    encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                    namespace="urn:examples:helloservice"
                    use="encoded"/>
        </output>
    </operation>
</binding>

<service name="Hello_Service">
    <documentation>WSDL File for HelloService</documentation>
    <port binding="tns:Hello_Binding" name="Hello_Port">
        <soap:address
                location="http://www.examples.com/SayHello/"/>
    </port>
</service>

绑定(bind).xjb

<jxb:bindings
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">

<jxb:bindings schemaLocation="*">
    <jxb:bindings node="//xs:complexType[@name='requestType']" required="false">
        <jxb:bindings node=".//xs:element[@name='label_1']" required="false">
            <jxb:property name="label2"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

最佳答案

您是否设置了 underscoreBinding 并尝试过?

<jxb:bindings schemaLocation="*">
    <jxb:globalBindings underscoreBinding="asCharInWord" />
</jxb:bindings>

关于java - 如何使用带有名称冲突的 maven-jaxb2-plugin 映射 wsdl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38736195/

相关文章:

java - Azure Tomcat 部署路径

java - 根据几个参数对类(class)内容进行排序

java - 如何为 JAXB2 Maven 插件指定 javax.xml.accessExternalSchema

jaxb2-annotate-plugin:向 XJC 类路径添加注释

maven-jaxb2-plugin forceRegenerate=false 不起作用?

java - 将系统属性从 Gradle 传递到 Spring Boot

Java:确定二维数组是否具有不连续的值

WPF - 当绑定(bind)不在字符串中时附加绑定(bind)调试

python - 无法在 Ubuntu 的 Python 中导入 FANN

c# - Windows Phone 应用程序中用户控件的绑定(bind)属性