最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot調(diào)用對(duì)方webService接口的幾種方法示例

 更新時(shí)間:2023年11月28日 10:40:01   作者:小小魚(yú)兒小小林  
平常我們開(kāi)發(fā)調(diào)用接口一般會(huì)用到幾種數(shù)據(jù)格式,比如有restful的,這個(gè)是目前最流行的,也是最簡(jiǎn)單開(kāi)發(fā)的,還有一種就是webservice數(shù)據(jù)格式,本文給大家介紹了幾種SpringBoot調(diào)用對(duì)方webService接口的方法,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下

前言

平常我們開(kāi)發(fā)調(diào)用接口一般會(huì)用到幾種數(shù)據(jù)格式,比如有restful的,這個(gè)是目前最流行的,也是最簡(jiǎn)單開(kāi)發(fā)的,還有一種就是webservice數(shù)據(jù)格式,這個(gè)應(yīng)該是很久以前的一些項(xiàng)目是用的這種

那什么是webservice呢,Web service是一個(gè)平臺(tái)獨(dú)立的,低耦合的,自包含的、基于可編程的web的應(yīng)用程序,可使用開(kāi)放的XML(標(biāo)準(zhǔn)通用標(biāo)記語(yǔ)言下的一個(gè)子集)標(biāo)準(zhǔn)來(lái)描述、發(fā)布、發(fā)現(xiàn)、協(xié)調(diào)和配置這些應(yīng)用程序,用于開(kāi)發(fā)分布式的互操作的應(yīng)用程序

在調(diào)用別人寫(xiě)好的webservice服務(wù)的時(shí)候,對(duì)方會(huì)給你一串schema文件(xsd文件)或者是wsdl結(jié)尾的地址,你訪問(wèn)wsdl地址和xsd文件是一樣的,比如下面的xsd格式的例子

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

二、如何調(diào)用webservice接口

調(diào)用方法一:

最簡(jiǎn)單的就是用這種方法,可以直接調(diào)對(duì)方的webService接口

/**
* 調(diào)用webservice接口
* 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/105275657
* 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
*/
public String sendWsdl(Object obj) {
        logger.info("--------調(diào)用webservice接口begin-------");
        // 創(chuàng)建動(dòng)態(tài)客戶(hù)端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
 
        //對(duì)方的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     * 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
            Object[] objects1= client.invoke(qName, "aaa","bbb"); //參數(shù)1,參數(shù)2,參數(shù)3......按順序放就看可以
 
            json = JSONObject.toJSONString(objects1[0]);
            System.out.println("返回?cái)?shù)據(jù):" + json.toString());
 
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("服務(wù)器斷開(kāi)連接,請(qǐng)稍后再試");
        }
        logger.info("--------調(diào)用webservice接口end-------");
        return json;
 
 
    }

調(diào)用方法二:

得借助開(kāi)發(fā)工具生成代碼,比如myEclipse 和 idea 工具

myEclipse生成的例子:

myEclipse根據(jù)xsd文件生成webservice代碼教程

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

看我截圖中顯示的一個(gè)文件,因?yàn)槲野褀sdl文件是放在D盤(pán)目錄下, 然后生成的,如果你們是直接用對(duì)方url生成的,這里應(yīng)該就是對(duì)方的url地址,當(dāng)然你也可以跟我一樣,放在本地生成,然后改成對(duì)方的地址,也是可以的。這個(gè)智者見(jiàn)智。

通過(guò)myeclipse生成上面的代碼之后,不一定就要在myeclipse上面開(kāi)發(fā),可以copy上面9個(gè)這些代碼到任何項(xiàng)目地方去,比如idea中,然后就可以通過(guò)下面的代碼去調(diào)用對(duì)方

/**
*調(diào)用webservice接口
*原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/105275657
* 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
*/
public String sendWsdlWebService(String aaa,String bbb) {
        logger.info("--------調(diào)用webservice查詢(xún)接口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 的簡(jiǎn)寫(xiě)
            //2.構(gòu)成:由名字空間(namespace)前綴(prefix)以及冒號(hào)(:),還有一個(gè)元素名稱(chēng)構(gòu)成
            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     * 其他均為盜版,公眾號(hào):靈兒的筆記(zygxsq)
            IPowerAlarm port = service.getPort(IPowerAlarm.class);
            PowerAlarmRsp powerAlarmRsp = port.queryPowerAlarm(aaa, bbb);
 
            json = JSONObject.toJSONString(powerAlarmRsp);
           // System.out.println("111返回?cái)?shù)據(jù):" + json.toString());
 
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.info("--------調(diào)用webservice查詢(xún)接口end-------");
        return json;
 
    }

idea生成的例子:

當(dāng)然,idea也是可以生成代碼的,只是相對(duì)myeclipse的生成比較麻煩,要引入一堆的maven,然后才能生成,這里我就不寫(xiě)了,我就在這里寫(xiě)一下要注意的一點(diǎn):要引入的maven,就是下面這一堆,而且生成代碼后,要注釋掉這些maven,或者去掉這些maven,不然你每編譯一次,就會(huì)重新生成一份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>

有很多留言或者私信我的朋友們,都咨詢(xún)用到的import 和dependence

我就貼在下面了

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
 
//-----主要下面這些-------
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.6</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>
 
 
        <!-- WebSocket -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <!--websocket作為客戶(hù)端-->
        <dependency>
            <groupId>org.java-websocket</groupId>
            <artifactId>Java-WebSocket</artifactId>
            <version>1.3.5</version>
        </dependency>
 
 <!--webservice-->
        <dependency>
            <groupId>com.tetragramato</groupId>
            <artifactId>custom-spring-boot-resttemplate</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency>
 
        <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>

以上就是SpringBoot調(diào)用對(duì)方webService接口的幾種方法示例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot調(diào)用webService的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringBoot登錄攔截配置詳解(實(shí)測(cè)可用)

    SpringBoot登錄攔截配置詳解(實(shí)測(cè)可用)

    這篇文章主要介紹了SpringBoot登錄攔截配置詳解(實(shí)測(cè)可用),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • java去除if...else的七種方法總結(jié)

    java去除if...else的七種方法總結(jié)

    相信小伙伴一定看過(guò)多篇怎么去掉?if...else?的文章,也知道大家都很有心得,知道多種方法來(lái)去掉?if...else?,本文為大家整理了7個(gè)常用的方法,希望對(duì)大家有所幫助
    2023-11-11
  • JavaWeb實(shí)現(xiàn)學(xué)生管理系統(tǒng)的超詳細(xì)過(guò)程

    JavaWeb實(shí)現(xiàn)學(xué)生管理系統(tǒng)的超詳細(xì)過(guò)程

    學(xué)生信息管理系統(tǒng)是針對(duì)學(xué)校人事處的大量業(yè)務(wù)處理工作而開(kāi)發(fā)的管理軟件,主要用于學(xué)校學(xué)生信息管理,下面這篇文章主要給大家介紹了關(guān)于JavaWeb實(shí)現(xiàn)學(xué)生管理系統(tǒng)的超詳細(xì)過(guò)程,需要的朋友可以參考下
    2023-05-05
  • Spring Boot集成MinIO對(duì)象存儲(chǔ)服務(wù)器操作步驟

    Spring Boot集成MinIO對(duì)象存儲(chǔ)服務(wù)器操作步驟

    通過(guò)Spring Boot集成MinIO,你可以在應(yīng)用中方便地進(jìn)行文件的存儲(chǔ)和管理,本文給大家分享Spring Boot集成MinIO對(duì)象存儲(chǔ)服務(wù)器詳細(xì)操作步驟,感興趣的朋友一起看看吧
    2024-01-01
  • ShardingSphere數(shù)據(jù)庫(kù)讀寫(xiě)分離算法及測(cè)試示例詳解

    ShardingSphere數(shù)據(jù)庫(kù)讀寫(xiě)分離算法及測(cè)試示例詳解

    這篇文章主要為大家介紹了ShardingSphere數(shù)據(jù)庫(kù)讀寫(xiě)分離算法及測(cè)試示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 面試題:Java中如何停止線程的方法

    面試題:Java中如何停止線程的方法

    這篇文章主要介紹了Java中如何停止線程的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java Stream流使用案例深入詳解

    Java Stream流使用案例深入詳解

    這篇文章主要介紹了Java Stream流使用案例詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2025-04-04
  • 詳解Spring 注解之@Import 注入的各種花活

    詳解Spring 注解之@Import 注入的各種花活

    這篇文章主要介紹了詳解Spring 注解之@Import 注入的各種花活,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Java數(shù)組集合的深度復(fù)制代碼實(shí)例

    Java數(shù)組集合的深度復(fù)制代碼實(shí)例

    這篇文章主要介紹了Java數(shù)組集合的深度復(fù)制代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • 基于java servlet過(guò)濾器和監(jiān)聽(tīng)器(詳解)

    基于java servlet過(guò)濾器和監(jiān)聽(tīng)器(詳解)

    下面小編就為大家?guī)?lái)一篇基于java servlet過(guò)濾器和監(jiān)聽(tīng)器(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10

最新評(píng)論

朔州市| 郓城县| 太康县| 宜川县| 洞头县| 修文县| 景洪市| 苗栗市| 阿拉尔市| 大港区| 瑞金市| 黑龙江省| 遵化市| 雷山县| 德令哈市| 嘉鱼县| 洛隆县| 民勤县| 龙胜| 宝丰县| 三门县| 平遥县| 仁化县| 句容市| 乌审旗| 东山县| 永川市| 赤峰市| 轮台县| 荣成市| 蕲春县| 高要市| 泰来县| 福建省| 翁源县| 阳山县| 三江| 荔波县| 盐边县| 麻江县| 南乐县|