简体   繁体   English

在WSDL之外的JAXB绑定XSD

[英]JAXB Binding for XSD outside WSDL

We are generating java with wsdl2java from third party WSDL (means we cant modify him). 我们使用来自第三方WSDL的wsdl2java生成java(意味着我们无法修改他)。 WSDL contain: WSDL包含:

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

In this xsdschema are elements with nillable="true" and generator reports conflicts (duplications) in ObjectFactory. 在这个xsdschema中是具有nillable =“true”的元素,并且生成器在ObjectFactory中报告冲突(重复)。 We try to use binding generateElementProperty="false". 我们尝试使用绑定generateElementProperty =“false”。 But in binding definition that is defined for WSDL, generator ignores it, and when defining bindings for xsd WSDL2Java said, that the XSD is not part of compilation. 但是在为WSDL定义的绑定定义中,生成器忽略它,并且在为xsd WSDL2Java定义绑定时说,XSD不是编译的一部分。 How to solve it? 怎么解决?

XJB for WSDL (generateElementProperty is ignored - still duplication error in ObjectFactory): WSJ的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 (error: XSD is not part of compilation): 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> 

Maven: Maven的:

            <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>

I suggest you this strategy.... 我建议你这个策略....

use this config to generate the API and exclude the XSD classes 使用此配置生成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>

use this plugin to generate the XSD classes with binding configuration 使用此插件生成具有绑定配置的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>

use this plugin to add resources 使用此插件添加资源

<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>

I used this strategy to divide the data model from APIs 我使用此策略将数据模型与API分开

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM