Java JAX-WS:独立于WSDL编译模式

Java JAX-WS:独立于WSDL编译模式,java,xsd,jaxb,wsdl,jax-ws,Java,Xsd,Jaxb,Wsdl,Jax Ws,如果有一个在多个Web服务中使用的相当大的模式,那么我想将XSD编译与WSDL编译分开。在一个简化的示例中,在一个步骤中编译可以工作: $ wsimport -verbose service.wsdl parsing WSDL... Generating code... org/example/wsdl/mysvc/MySvcPortType.java org/example/wsdl/mysvc/MySvcService.java org/example/ns1/Element1.java

如果有一个在多个Web服务中使用的相当大的模式,那么我想将XSD编译与WSDL编译分开。在一个简化的示例中,在一个步骤中编译可以工作:

$ wsimport -verbose service.wsdl
parsing WSDL...

Generating code...

org/example/wsdl/mysvc/MySvcPortType.java
org/example/wsdl/mysvc/MySvcService.java
org/example/ns1/Element1.java
org/example/ns1/ObjectFactory.java
org/example/ns1/package-info.java
...
编译xsd并使用生成的事件文件不起作用:

$ wsimport -b schema3.episode service.wsdl  
parsing WSDL...

[ERROR] Schema descriptor {http://www.example.org/ns1}element1 in
message part "part1" is not defined and could not be bound to Java.
Perhaps the schema descriptor {http://www.example.org/ns1}element1 is
not defined in the schema imported/included in the WSDL. You can
either add such imports/includes or run wsimport and provide the
schema location using -b switch.   line 9 of
file:...jaxepisode_element/service.wsdl
那么,如何在wsimport中使用预编译模式呢

增编: 当包含在另一个模式中时使用该插曲会起作用并避免新的编译(d3.jar包含来自schema3+schema3.插曲的genrated类作为META-INF/sun jaxb.插曲):

如果没有预编译包:

$ xjc schema4.xsd  -extension
parsing a schema...
compiling a schema...
org/example/ns1/Element1.java
org/example/ns1/ObjectFactory.java
org/example/ns1/package-info.java
org/example/ns2/Element2.java
org/example/ns2/ObjectFactory.java
org/example/ns2/package-info.java
WSDL:


schema3.xsd:

<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/ns1" 
    xmlns:tns="http://www.example.org/ns1" 
    elementFormDefault="qualified">
  <element name="element1" >
    <complexType >
        <sequence>
                <element name="name" type="string" />
        </sequence>
     </complexType>
   </element>
</schema>

schema4.xsd:

<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/ns2" 
    xmlns:tns="http://www.example.org/ns2" 
    xmlns:ns1="http://www.example.org/ns1" 
    elementFormDefault="qualified">

    <import namespace="http://www.example.org/ns1" schemaLocation="schema3.xsd"/> 
    <element name="element2" >
      <complexType >
        <sequence>
          <element ref="ns1:element1" />
        </sequence>
       </complexType>
     </element>
</schema>

生成的事件文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="2.1" xmlns="http://java.sun.com/xml/ns/jaxb">
  <bindings scd="x-schema::tns" xmlns:tns="http://www.example.org/ns1">
    <schemaBindings map="false">
      <package name="org.example.ns1"/>
    </schemaBindings>
    <bindings scd="tns:element1">
      <class ref="org.example.ns1.Element1"/>
    </bindings>
  </bindings>
</bindings>

您可以使用maven,分别使用ApacheCXF生成

我有一个例子可以帮助你

特性:

 <properties>
    <apache.cxf.version>3.0.4</apache.cxf.version>
    <cxf-codegen-plugin.version>3.0.4</cxf-codegen-plugin.version>
    <cxf-xjc-plugin.version>3.0.3</cxf-xjc-plugin.version>
 </properties>

3.0.4
3.0.4
3.0.3
依赖项:

 <dependencies>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>${apache.cxf.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>${apache.cxf.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-databinding-jaxb</artifactId>
        <version>${apache.cxf.version}</version>
        <exclusions>
            <exclusion>
                <groupId>com.sun.xml.bind</groupId>
                <artifactId>jaxb-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.xml.bind</groupId>
                <artifactId>jaxb-impl</artifactId>
            </exclusion>
            <exclusion>
                <groupId>xalan</groupId>
                <artifactId>xalan</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
 </dependencies>

org.apache.cxf
cxf rt前端jaxws
${apache.cxf.version}
org.apache.cxf
cxf rt传输http
${apache.cxf.version}
org.apache.cxf
cxf rt数据绑定jaxb
${apache.cxf.version}
com.sun.xml.bind
jaxb内核
com.sun.xml.bind
jaxb impl
沙兰
沙兰
运行-->mvn清洁安装-生成nfe服务

   <profile>
        <id>generates-nfe-services</id>
        <activation>
            <property>
                <name>generates-nfe-services</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-codegen-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>generate-sources</id>
                            <phase>generate-sources</phase>
                            <configuration>
                                <!--sourceRoot>${project.build.directory}/generated-sources</sourceRoot-->
                                <sourceRoot>${basedir}/src/main/java</sourceRoot>
                                <defaultOptions>
                                    <extraargs>
                                        <extraarg>-validate</extraarg>
                                        <extraarg>-client</extraarg>
                                        <extraarg>-verbose</extraarg>
                                        <extraarg>-xjc-npa</extraarg>
                                        <extraarg>-xjc-verbose</extraarg>
                                        <extraarg>-xjc-extension</extraarg>
                                        <extraarg>-exsh</extraarg>
                                        <extraarg>true</extraarg>
                                        <extraarg>-keep</extraarg>
                                    </extraargs>
                                </defaultOptions>
                                <wsdlOptions>
                                    <wsdlOption>
                                        <wsdl>
                                            ${basedir}/src/main/wsdl/br/gov/rs/sefaz/nfe/homologacao/NfeConsultaCadastro.wsdl
                                        </wsdl>
                                        <extraargs>
                                            <extraarg>-p</extraarg>
                                            <extraarg>br.gov.rs.sefaz.nfe.consulta.cadastro</extraarg>
                                        </extraargs>
                                    </wsdlOption>
                                </wsdlOptions>
                            </configuration>
                            <goals>
                                <goal>wsdl2java</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

生成nfe服务
生成nfe服务
org.apache.cxf
cxf-codegen插件
生成源
生成源
${basedir}/src/main/java
-证实
-客户
-冗长的
-xjc npa
-详细的
-xjc扩展
-埃克斯
真的
-保持
${basedir}/src/main/wsdl/br/gov/rs/sefaz/nfe/homologao/NfeConsultaCadastro.wsdl
-p
br.gov.rs.sefaz.nfe.consulta.cadastro
wsdl2java
运行-->mvn清洁安装-生成布局nfe

    <profile>
        <id>generates-layouts-nfe</id>
        <activation>
            <property>
                <name>generates-layouts-nfe</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-xjc-plugin</artifactId>
                    <version>${cxf-xjc-plugin.version}</version>
                    <executions>
                        <execution>
                            <id>generate-sources</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>xsdtojava</goal>
                            </goals>
                            <configuration>
                                <!--sourceRoot>${basedir}/target/generated-sources</sourceRoot-->
                                <sourceRoot>${basedir}/src/main/java</sourceRoot>
                                <xsdOptions>
                                    <xsdOption>
                                        <xsd>${basedir}/src/main/schemas/nfe/PL_008f/leiauteConsSitNFe_v3.10.xsd
                                        </xsd>
                                        <packagename>br.inf.portalfiscal.nfe.v310.leiaute.consulta.situacao.nfe
                                        </packagename>
                                        <extension>true</extension>
                                        <extensionArgs>
                                            <!--extensionArg>-npa</extensionArg-->
                                            <extensionArg>-extension</extensionArg>
                                        </extensionArgs>
                                    </xsdOption>
                                </xsdOptions>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

生成布局nfe
生成布局nfe
org.apache.cxf
cxf xjc插件
${cxf xjc plugin.version}
生成源
生成源
xsdtojava
${basedir}/src/main/java
${basedir}/src/main/schemas/nfe/PL_008f/leiauteconsissitnfe_v3.10.xsd
br.inf.portalfiscal.nfe.v310.leiaute.consulta.situacao.nfe
真的
-延伸
您可以创建一个maven项目以仅生成wsdl,另一个项目仅生成xsd

注:

如果他的项目不需要使用WSDL,请切换到json rest api

休息一下,快乐起来\o/


我希望这能有所帮助。

你看过吗?我以前没有看过,但读完后,我认为解决方案不适用于我的情况。关于coderanch trrwad:我已经为我的消息使用了元素,而不是类型。重新分级其他问题:我有2001模式名称空间,我的名称属性中没有名称空间,
type=“string”
是正确的,因为xmls模式是默认名称空间。我想我会尝试一下CXF。Json并不是一个真正的选项
   <profile>
        <id>generates-nfe-services</id>
        <activation>
            <property>
                <name>generates-nfe-services</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-codegen-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>generate-sources</id>
                            <phase>generate-sources</phase>
                            <configuration>
                                <!--sourceRoot>${project.build.directory}/generated-sources</sourceRoot-->
                                <sourceRoot>${basedir}/src/main/java</sourceRoot>
                                <defaultOptions>
                                    <extraargs>
                                        <extraarg>-validate</extraarg>
                                        <extraarg>-client</extraarg>
                                        <extraarg>-verbose</extraarg>
                                        <extraarg>-xjc-npa</extraarg>
                                        <extraarg>-xjc-verbose</extraarg>
                                        <extraarg>-xjc-extension</extraarg>
                                        <extraarg>-exsh</extraarg>
                                        <extraarg>true</extraarg>
                                        <extraarg>-keep</extraarg>
                                    </extraargs>
                                </defaultOptions>
                                <wsdlOptions>
                                    <wsdlOption>
                                        <wsdl>
                                            ${basedir}/src/main/wsdl/br/gov/rs/sefaz/nfe/homologacao/NfeConsultaCadastro.wsdl
                                        </wsdl>
                                        <extraargs>
                                            <extraarg>-p</extraarg>
                                            <extraarg>br.gov.rs.sefaz.nfe.consulta.cadastro</extraarg>
                                        </extraargs>
                                    </wsdlOption>
                                </wsdlOptions>
                            </configuration>
                            <goals>
                                <goal>wsdl2java</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>generates-layouts-nfe</id>
        <activation>
            <property>
                <name>generates-layouts-nfe</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-xjc-plugin</artifactId>
                    <version>${cxf-xjc-plugin.version}</version>
                    <executions>
                        <execution>
                            <id>generate-sources</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>xsdtojava</goal>
                            </goals>
                            <configuration>
                                <!--sourceRoot>${basedir}/target/generated-sources</sourceRoot-->
                                <sourceRoot>${basedir}/src/main/java</sourceRoot>
                                <xsdOptions>
                                    <xsdOption>
                                        <xsd>${basedir}/src/main/schemas/nfe/PL_008f/leiauteConsSitNFe_v3.10.xsd
                                        </xsd>
                                        <packagename>br.inf.portalfiscal.nfe.v310.leiaute.consulta.situacao.nfe
                                        </packagename>
                                        <extension>true</extension>
                                        <extensionArgs>
                                            <!--extensionArg>-npa</extensionArg-->
                                            <extensionArg>-extension</extensionArg>
                                        </extensionArgs>
                                    </xsdOption>
                                </xsdOptions>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>