spring boot integration web service client automatically generates client code according to wsdl file

Add dependency
The client also needs to add dependency first:

<dependency>
    <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-ws</artifactId>
</dependency>
<dependency>
   <groupId>wsdl4j</groupId>
  <artifactId>wsdl4j</artifactId>
</dependency>

Get wsdl file
The server starts with an xsd file and the client starts with a wsdl file.
Getting the wsdl file is also very simple. You can access the web service address with a browser and save it as. Of course, you can also directly use url address to generate code, but I'm used to saving it locally and generating it later.
The contents of the complete wsdl file are as follows:

<wsdl:definitions 
   xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:sch="http://www.dexcoder.com/ws" 
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
    xmlns:tns="http://www.dexcoder.com/ws" targetNamespace="http://www.dexcoder.com/ws">
    <wsdl:types>
       <xs:schema 
            xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.dexcoder.com/ws">
            <xs:element name="getCountryRequest">
               <xs:complexType>
                  <xs:sequence>
                       <xs:element name="name" type="xs:string"/>
                  </xs:sequence>
               </xs:complexType>
           </xs:element>
         <xs:element name="getCountryResponse">
               <xs:complexType>
                  <xs:sequence>
                      <xs:element name="country" type="tns:country"/>
                 </xs:sequence>
              </xs:complexType>
           </xs:element>
           <xs:complexType name="country">
                <xs:sequence>
                   <xs:element name="name" type="xs:string"/>
                   <xs:element name="population" type="xs:int"/>
                    <xs:element name="capital" type="xs:string"/>
                   <xs:element name="currency" type="tns:currency"/>
               </xs:sequence>
           </xs:complexType>
            <xs:simpleType name="currency">
               <xs:restriction base="xs:string">
                    <xs:enumeration value="GBP"/>
                   <xs:enumeration value="EUR"/>
                   <xs:enumeration value="PLN"/>
              </xs:restriction>
            </xs:simpleType>
       </xs:schema>
    </wsdl:types>
    <wsdl:message name="getCountryResponse">
      <wsdl:part element="tns:getCountryResponse" name="getCountryResponse"></wsdl:part>
   </wsdl:message>
   <wsdl:message name="getCountryRequest">
       <wsdl:part element="tns:getCountryRequest" name="getCountryRequest"></wsdl:part>
    </wsdl:message>
   <wsdl:portType name="CountriesPort">
        <wsdl:operation name="getCountry">
            <wsdl:input message="tns:getCountryRequest" name="getCountryRequest"></wsdl:input>
49.            <wsdl:output message="tns:getCountryResponse" name="getCountryResponse"></wsdl:output>
       </wsdl:operation>
  </wsdl:portType>
    <wsdl:binding name="CountriesPortSoap11" type="tns:CountriesPort">
      <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
      <wsdl:operation name="getCountry">
           <soap:operation soapAction=""/>
            <wsdl:input name="getCountryRequest">
               <soap:body use="literal"/>
          </wsdl:input>
           <wsdl:output name="getCountryResponse">
               <soap:body use="literal"/>
          </wsdl:output>
       </wsdl:operation>
   </wsdl:binding>
    <wsdl:service name="CountriesPortService">
       <wsdl:port binding="tns:CountriesPortSoap11" name="CountriesPortSoap11">
           <soap:address/>
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>

Add jaxb2 plug-in of maven to generate code
Similar to the server generating code based on xsd, the client can generate code based on wsdl. maven plug-in depends on:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
   <version>0.12.3</version>
    <executions>
        <execution>
            <goals>
               <goal>generate</goal>
            </goals>
       </execution>
    </executions>
.    <configuration>
       <schemaLanguage>WSDL</schemaLanguage>
       <generatePackage>com.dexcoder.ws</generatePackage>
        <generateDirectory>${basedir}/src/main/java</generateDirectory>
      <schemas>
           <schema>
                <fileset>
                   <!-- Defaults to schemaDirectory. -->
                    <directory>${basedir}/src/main/resources/schemas</directory>
                   <!-- Defaults to schemaIncludes. -->
                   <includes>
                        <include>*.wsdl</include>
                  </includes>
                    <!-- Defaults to schemaIncludes -->
                    <!--<excludes>-->
                 <!--<exclude>*.xs</exclude>-->
                 <!--</excludes>-->
.                </fileset>
               <!--<url>http://localhost:8080/ws/countries.wsdl</url>-->
            </schema>
        </schemas>
    </configuration>
</plugin>

Then execute mvn install to generate the corresponding file.
Steps of mvn install:
Step 1: enter the project and directory
Step 2: enter cmd in the address bar of the directory
Step 3: enter mvn install enter

Tags: Maven Spring Java

Posted on Sat, 30 Nov 2019 08:20:20 -0800 by sirfartalot