[親測可用]springBoot調用對方webService接口的幾種方法示例

目錄

前言

一、需要用到的maven

二、如何調用webservice接口

調用方法一:

調用方法二:

myEclipse生成的例子:

idea生成的例子:


前言

平常我們開發調用接口一般會用到幾種數據格式,比如有restful的,這個是目前最流行的,也是最簡單開發的,還有一種就是webservice數據格式,這個應該是很久以前的一些項目是用的這種

那什麼是webservice呢,Web service是一個平臺獨立的,低耦合的,自包含的、基於可編程的web的應用程序,可使用開放的XML(標準通用標記語言下的一個子集)標準來描述、發佈、發現、協調和配置這些應用程序,用於開發分佈式的互操作的應用程序

在調用別人寫好的webservice服務的時候,對方會給你一串schema文件(xsd文件)或者是wsdl結尾的地址,你訪問wsdl地址和xsd文件是一樣的,比如下面的xsd格式的例子

當然還有很多其他的xsd例子,點擊查看:https://my.oschina.net/CraneHe/blog/183471

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://xxx.zygxsq.cn/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="PowerAlarmImplService" targetNamespace="http://xxx.zygxsq.cn/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://xxx.zygxsq.cn/" elementFormDefault="unqualified" targetNamespace="http://xxx.zygxsq.cn/" version="1.0">
<xs:element name="queryPowerAlarm" type="tns:queryPowerAlarm"/>
<xs:element name="queryPowerAlarmResponse" type="tns:queryPowerAlarmResponse"/>
<xs:complexType name="queryPowerAlarm">
<xs:sequence>
<xs:element minOccurs="0" name="alarmId" type="xs:string"/>
<xs:element minOccurs="0" name="eventTime" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="queryPowerAlarmResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:powerAlarmRsp"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="powerAlarmRsp">
<xs:complexContent>
<xs:extension base="tns:baseRsp">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="responseList" nillable="true" type="tns:powerAlarm"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="baseRsp">
<xs:sequence>
<xs:element minOccurs="0" name="errorInfo" type="xs:string"/>
<xs:element name="resultCode" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="powerAlarm">
<xs:sequence>
<xs:element minOccurs="0" name="alarmId" type="xs:string"/>
<xs:element minOccurs="0" name="alarmStatus" type="xs:string"/>
<xs:element minOccurs="0" name="canelTime" type="xs:string"/>
<xs:element minOccurs="0" name="eventTime" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="queryPowerAlarmResponse">
<wsdl:part element="tns:queryPowerAlarmResponse" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="queryPowerAlarm">
<wsdl:part element="tns:queryPowerAlarm" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="IPowerAlarm">
<wsdl:operation name="queryPowerAlarm">
<wsdl:input message="tns:queryPowerAlarm" name="queryPowerAlarm"> </wsdl:input>
<wsdl:output message="tns:queryPowerAlarmResponse" name="queryPowerAlarmResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="PowerAlarmImplServiceSoapBinding" type="tns:IPowerAlarm">
<soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="queryPowerAlarm">
<soap12:operation soapAction="" style="document"/>
<wsdl:input name="queryPowerAlarm">
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output name="queryPowerAlarmResponse">
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="PowerAlarmImplService">
<wsdl:port binding="tns:PowerAlarmImplServiceSoapBinding" name="PowerAlarmImplPort">
<soap12:address location="http://11.111.11.111:9556/xxx/ws/powerAlarmWs"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

 

一、需要用到的maven

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

二、如何調用webservice接口

調用方法一:

最簡單的就是用這種方法,可以直接調對方的webService接口

/**
* 調用webservice接口
* 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/105275657
* 其他均爲盜版,公衆號:靈兒的筆記(zygxsq)
*/
public String sendWsdl(Object obj) {
        logger.info("--------調用webservice接口begin-------");
        // 創建動態客戶端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

        //對方的wsdl地址
        Client client = dcf.createClient("http://xx
.xxx.xx.xx:9556/xxx/ws/getAlarmWs?wsdl");
        String json = null;
        try {

            QName qName = new QName("http://xx.zygxsq.cn/", "getAlarmWs");                                                //*原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/105275657     * 其他均爲盜版,公衆號:靈兒的筆記(zygxsq)
            Object[] objects1= client.invoke(qName, "aaa","bbb"); //參數1,參數2,參數3......按順序放就看可以

            json = JSONObject.toJSONString(objects1[0]);
            System.out.println("返回數據:" + json.toString());

        } catch (Exception e) {
            e.printStackTrace();
            logger.info("服務器斷開連接,請稍後再試");
        }
        logger.info("--------調用webservice接口end-------");
        return json;


    }

 

調用方法二:

得藉助開發工具生成代碼,比如myEclipse 和 idea 工具

myEclipse 生成的教程可以看下這篇文章:https://www.cnblogs.com/demojie/archive/2017/08/24/java_webservice.html

idea 生成的教程可以看下這篇文章:https://blog.csdn.net/weixin_40699910/article/details/103399292

 

myEclipse生成的例子:

myEclipse根據xsd文件生成webservice代碼教程

 

 

1、如果選擇本地的wsdl文件,生成後就是這麼一堆代碼,如圖所示

 

看我截圖中顯示的一個文件,因爲我把wsdl文件是放在D盤目錄下, 然後生成的,如果你們是直接用對方url生成的,這裏應該就是對方的url地址,當然你也可以跟我一樣,放在本地生成,然後改成對方的地址,也是可以的。這個智者見智。

 

通過myeclipse生成上面的代碼之後,不一定就要在myeclipse上面開發,可以copy上面9個這些代碼到任何項目地方去,比如idea中,然後就可以通過下面的代碼去調用對方

/**
*調用webservice接口
*原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/105275657
* 其他均爲盜版,公衆號:靈兒的筆記(zygxsq)
*/
public String sendWsdlWebService(String aaa,String bbb) {
        logger.info("--------調用webservice查詢接口begin-------");
        QueryPowerAlarmResponse queryPowerAlarmResponse=null;
        URL url;
        String json="";
        try {

            url = new URL("http://11.111.111.111:9556/xxx/ws/powerAlarmWs?wsdl");
            //Qnameqname是qualified name 的簡寫
            //2.構成:由名字空間(namespace)前綴(prefix)以及冒號(:),還有一個元素名稱構成
            QName qname=new QName("http://xxx.zygxsq.cn/","PowerAlarmImplService");
            javax.xml.ws.Service service= javax.xml.ws.Service.create(url, qname);                                 //*原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/105275657     * 其他均爲盜版,公衆號:靈兒的筆記(zygxsq)
            IPowerAlarm port = service.getPort(IPowerAlarm.class);
            PowerAlarmRsp powerAlarmRsp = port.queryPowerAlarm(aaa, bbb);

            json = JSONObject.toJSONString(powerAlarmRsp);
           // System.out.println("111返回數據:" + json.toString());

        }catch (Exception e){
            e.printStackTrace();
        }
        logger.info("--------調用webservice查詢接口end-------");
        return json;

    }

 

idea生成的例子:

當然,idea也是可以生成代碼的,只是相對myeclipse的生成比較麻煩,要引入一堆的maven,然後才能生成,

具體完整的可以參考這篇文章:https://blog.csdn.net/weixin_40699910/article/details/103399292

 

這裏我就不寫了,我就在這裏寫一下要注意的一點:要引入的maven,就是下面這一堆,而且生成代碼後,要註釋掉這些maven,或者去掉這些maven,不然你每編譯一次,就會重新生成一份webSocket的代碼。

  <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <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>
                <directory>${basedir}/src/main/resources</directory>
                <includes>
                <include>*.wsdl</include>
              </fileset>
            </schema>
          </schemas>
        </configuration>
      </plugin>
    </plugins>

參考文章 

https://www.cnblogs.com/demojie/archive/2017/08/24/java_webservice.html

https://blog.csdn.net/weixin_40699910/article/details/103399292

感謝原作者的分享,讓技術人能夠更快的解決問題 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章