spring boot 整合webservice客户端 根据wsdl文件自动生成客户端代码

添加依赖
客户端,同样的需要先添加依赖:

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

获取wsdl文件
服务端由一个xsd文件开始,客户端则是由一个wsdl文件开始。
获取wsdl文件也十分简单,用浏览器访问web service地址,然后另存为即可。当然也可以直接用url地址来生成代码,只不过我习惯本地另存为后再生成。
完整的wsdl文件内容如下:

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

添加maven的jaxb2插件生成代码
跟服务端根据xsd来生成代码类似,客户端同样可以根据wsdl来生成代码。maven插件依赖:

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

然后执行mvn install 来生成对应的文件。
mvn install 执行的步骤:
第一步:进入项目跟目录
第二步:在跟目录的地址栏中输入cmd回车
第三步骤:输入mvn install回车

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Spring Boot可以使用JAX-WS或者Spring Web Services(Spring-WS)来调用SOAP Web Service接口,也可以使用RestTemplate来调用RESTful Web Service接口。 以下是使用Spring-WS调用SOAP Web Service接口的步骤: 1. 引入Spring-WS和JAXB相关依赖 ```xml <dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> <version>3.0.7.RELEASE</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>3.0.0</version> </dependency> ``` 2. 配置WebServiceTemplate 在配置类中添加WebServiceTemplate的Bean,并设置WebServiceTemplate的Marshaller和Unmarshaller,这里使用Jaxb2Marshaller ```java @Configuration public class WebServiceConfig { @Bean public Jaxb2Marshaller marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath("com.example.webservice.demo.wsdl"); return marshaller; } @Bean public WebServiceTemplate webServiceTemplate() { WebServiceTemplate template = new WebServiceTemplate(); template.setMarshaller(marshaller()); template.setUnmarshaller(marshaller()); template.setDefaultUri("http://localhost:8080/ws"); return template; } } ``` 3. 调用WebService 使用WebServiceTemplate的marshalSendAndReceive方法来发送SOAP请求并接收响应,示例代码如下: ```java @Autowired private WebServiceTemplate webServiceTemplate; public void callWebService() { GetCountryRequest request = new GetCountryRequest(); request.setName("Spain"); GetCountryResponse response = (GetCountryResponse) webServiceTemplate.marshalSendAndReceive(request); System.out.println(response.getCountry().getCapital()); } ``` 以上就是使用Spring-WS调用SOAP Web Service接口的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值