2015-03-06 34 views
21

Jeśli ma raczej duży schemat, który jest używany w kilku usługach internetowych, dlatego chcę oddzielić kompilację XSD od kompilacji WSDL. W uproszczonym przykładzie kompilacji jednoetapowo działa:JAX-WS: Skompiluj schemat oddzielny od 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 
... 

Kompilacja xsd i stosując plik wynikowy epizod nie działa:

$ 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 

Więc jak mogę użyć schematu prekompilowanego w wsimport?

Uzupełnienie: Korzystanie epizod gdy w tym w kolejnych pracach schematu i unika nową kompilację (d3.jar zawiera genrated klas z schema3 + schema3.episode jako META-INF/słońca jaxb.episode):

$ xjc schema4.xsd d3.jar -extension 
parsing a schema... 

compiling a schema... 

org/example/ns2/Element2.java 
org/example/ns2/ObjectFactory.java 
org/example/ns2/package-info.java 

Bez pakietu skompilowany:

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

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.org/wsdl/MySvc" xmlns:ns="http://www.example.org/ns1" targetNamespace="http://example.org/wsdl/MySvc" name="MySvc"> 
    <types> 
    <xsd:schema> 
     <xsd:import namespace="http://www.example.org/ns1" schemaLocation="schema3.xsd"/> 
    </xsd:schema> 
    </types> 
    <message name="myOpRequest"> 
    <part name="part1" element="ns:element1"/> 
    </message> 
    <message name="myOpReply"> 
    <part name="part1" element="ns:element1"/> 
    </message> 
    <portType name="MySvcPortType"> 
    <operation name="myOp"> 
     <input name="input1" message="tns:myOpRequest"/> 
     <output name="output1" message="tns:myOpReply"/> 
    </operation> 
    </portType> 
    <binding name="MySvcBinding" type="tns:MySvcPortType"> 
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> 
    <operation name="myOp"> 
     <soap:operation/> 
     <input name="input1"> 
     <soap:body use="literal"/> 
     </input> 
     <output name="output1"> 
     <soap:body use="literal"/> 
     </output> 
    </operation> 
    </binding> 
    <service name="MySvcService"> 
    <port name="MySvcPort" binding="tns:MySvcBinding"> 
     <soap:address location="http://localhost:8080/"/> 
    </port> 
    </service> 
</definitions> 

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> 

Wygenerowany plik epizod:

<?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> 
+0

Widziałeś [to pytanie] (http://stackoverflow.com/q/1682479/1064325) i [ten wątek] (http://www.coderanch.com/t/460978/Web-Services/ java/usługa WSDL)? – falsarella

+1

Nie widziałem ich wcześniej, ale po ich przeczytaniu nie sądzę, aby rozwiązania pasowały do ​​mojej sprawy. Odnośnie coderanch-trrwad: używam już elementów do moich wiadomości, a nie typów. Ponowne oglĘ ... czenie innego pytania SO: Mam obszar nazw schematu z 2001 roku, nie mam przestrzeni nazw w moim imieniu-Atrybuty i 'typ =" napis "' jest prawidowy, poniewa xmls schema jest domyslnym obszarem nazw. – Drunix

Odpowiedz

6

Można użyć Maven, odrębnie dla każdej z Generowanie i Apache CXF.

Mam przykład, który może pomóc.

nieruchomości:

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

zależności:

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

Run -> mvn czystej instalacji -Pgenerates-NFE-services

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

Uruchom -> mvn czysta instalacja - Pgenerates-layouts-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> 

Można utworzyć projekt maven, który wygeneruje tylko kod wsdl, a drugi wygeneruje tylko plik xsd.

UWAGA:

Jeśli jego projekt nie wymaga stosowania WSDL, przejdź do JSON-REST API.

Skorzystaj z RESTU i bądź szczęśliwy. \ O/

Mam nadzieję, że to pomoże.

+1

Myślę, że dam CXF spróbować. Json nie jest tak naprawdę opcją dla. – Drunix