【问题标题】:JAXB Binding for XSD outside WSDLWSDL 外部 XSD 的 JAXB 绑定
【发布时间】:2014-10-30 11:37:36
【问题描述】:

我们正在使用来自第三方 WSDL 的 wsdl2java 生成 java(意味着我们不能修改他)。 WSDL 包含:

<wsdl:import namespace="http://somenamespace" location="xsdschema.xsd" /> 

在这个 xsdschema 中有 nillable="true" 的元素,并且生成器在 ObjectFactory 中报告冲突(重复)。我们尝试使用绑定 generateElementProperty="false"。但是在为 WSDL 定义的绑定定义中,生成器会忽略它,并且在为 xsd 定义绑定时 WSDL2Java 说,XSD 不是编译的一部分。如何解决?

用于 WSDL 的 XJB(generateElementProperty 被忽略 - ObjectFactory 中仍然存在重复错误):

<jaxws:bindings version="2.0"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
               wsdlLocation="wsdl3rd.wsdl">

    <jaxb:bindings>
        <jaxb:globalBindings generateElementProperty="false"/>
    </jaxb:bindings>
</jaxws:bindings>

XJB for XSD(错误:XSD 不是编译的一部分):

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

马文:

            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>${cxf.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                        <configuration>
                            <wsdlRoot>src/main/resources/wsdl</wsdlRoot>
                            <defaultOptions>
                                <bindingFiles>bindingFile>bindingFile.xjb</bindingFile>
                                </bindingFiles>
                            </defaultOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

【问题讨论】:

  • 你解决了这个问题吗?如果是,你是怎么解决的?

标签: binding jaxb xsd wsdl wsdl2java


【解决方案1】:

我建议你这个策略....

使用此配置生成 API 并排除 XSD 类

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${cxf.version}</version>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
                <wsdlOptions>
                    <wsdlOption>
                        <wsdl>${basedir}/pathOfYourWSDL.wsdl</wsdl>
                        <extraargs>
                            <extraarg>-nexclude</extraarg>
                            <extraarg>http://somenamespace</extraarg>                                   
                        </extraargs>
                    </wsdlOption>                           
                </wsdlOptions>
            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
    </executions>
</plugin>

使用此插件生成具有绑定配置的 XSD 类

           <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>
                    <forceRegenerate>true</forceRegenerate>
                    <bindingDirectory>${basedir}/pathOfYourXJB</bindingDirectory>
                    <bindingIncludes>
                        <include>yourXJB.xjb</include>
                    </bindingIncludes>
                    <schemas>
                        <schema>
                            <fileset>
                                <directory>${basedir}/pathOfYourXSD</directory>
                                <includes>
                                    <include>yourXSD.xsd</include>
                                </includes>
                            </fileset>
                        </schema>
                    </schemas>
                    <debug>true</debug>
                    <verbose>true</verbose>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.6.2</version>
                        </plugin>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics-annotate</artifactId>
                            <version>0.6.2</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>
                                <source>target/generated-sources/cxf</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

我使用这种策略将数据模型与 API 分开

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多