SpringBoot調(diào)用對方webService接口的幾種方法示例
前言
平常我們開發(fā)調(diào)用接口一般會用到幾種數(shù)據(jù)格式,比如有restful的,這個是目前最流行的,也是最簡單開發(fā)的,還有一種就是webservice數(shù)據(jù)格式,這個應(yīng)該是很久以前的一些項目是用的這種
那什么是webservice呢,Web service是一個平臺獨(dú)立的,低耦合的,自包含的、基于可編程的web的應(yīng)用程序,可使用開放的XML(標(biāo)準(zhǔn)通用標(biāo)記語言下的一個子集)標(biāo)準(zhǔn)來描述、發(fā)布、發(fā)現(xiàn)、協(xié)調(diào)和配置這些應(yīng)用程序,用于開發(fā)分布式的互操作的應(yīng)用程序
在調(diào)用別人寫好的webservice服務(wù)的時候,對方會給你一串schema文件(xsd文件)或者是wsdl結(jié)尾的地址,你訪問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)用方法一:
最簡單的就是用這種方法,可以直接調(diào)對方的webService接口
/** * 調(diào)用webservice接口 * 原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/105275657 * 其他均為盜版,公眾號:靈兒的筆記(zygxsq) */ public String sendWsdl(Object obj) { logger.info("--------調(diào)用webservice接口begin-------"); // 創(chuàng)建動態(tài)客戶端 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"); //參數(shù)1,參數(shù)2,參數(shù)3......按順序放就看可以 json = JSONObject.toJSONString(objects1[0]); System.out.println("返回數(shù)據(jù):" + json.toString()); } catch (Exception e) { e.printStackTrace(); logger.info("服務(wù)器斷開連接,請稍后再試"); } logger.info("--------調(diào)用webservice接口end-------"); return json; }
調(diào)用方法二:
得借助開發(fā)工具生成代碼,比如myEclipse 和 idea 工具
myEclipse生成的例子:
myEclipse根據(jù)xsd文件生成webservice代碼教程
1、如果選擇本地的wsdl文件,生成后就是這么一堆代碼,如圖所示
看我截圖中顯示的一個文件,因為我把wsdl文件是放在D盤目錄下, 然后生成的,如果你們是直接用對方url生成的,這里應(yīng)該就是對方的url地址,當(dāng)然你也可以跟我一樣,放在本地生成,然后改成對方的地址,也是可以的。這個智者見智。
通過myeclipse生成上面的代碼之后,不一定就要在myeclipse上面開發(fā),可以copy上面9個這些代碼到任何項目地方去,比如idea中,然后就可以通過下面的代碼去調(diào)用對方
/** *調(diào)用webservice接口 *原文章鏈接:https://blog.csdn.net/qq_27471405/article/details/105275657 * 其他均為盜版,公眾號:靈兒的筆記(zygxsq) */ public String sendWsdlWebService(String aaa,String bbb) { logger.info("--------調(diào)用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.構(gòu)成:由名字空間(namespace)前綴(prefix)以及冒號(:),還有一個元素名稱構(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 * 其他均為盜版,公眾號:靈兒的筆記(zygxsq) IPowerAlarm port = service.getPort(IPowerAlarm.class); PowerAlarmRsp powerAlarmRsp = port.queryPowerAlarm(aaa, bbb); json = JSONObject.toJSONString(powerAlarmRsp); // System.out.println("111返回數(shù)據(jù):" + json.toString()); }catch (Exception e){ e.printStackTrace(); } logger.info("--------調(diào)用webservice查詢接口end-------"); return json; }
idea生成的例子:
當(dāng)然,idea也是可以生成代碼的,只是相對myeclipse的生成比較麻煩,要引入一堆的maven,然后才能生成,這里我就不寫了,我就在這里寫一下要注意的一點(diǎn):要引入的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>
有很多留言或者私信我的朋友們,都咨詢用到的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作為客戶端--> <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)用對方webService接口的幾種方法示例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot調(diào)用webService的資料請關(guān)注腳本之家其它相關(guān)文章!
- Spring boot webService使用方法解析
- SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類型)
- SpringBoot整合WebService的實(shí)現(xiàn)示例
- SpringBoot創(chuàng)建WebService方法詳解
- SpringBoot調(diào)用第三方WebService接口的兩種方法
- SpringBoot整合WebService的實(shí)戰(zhàn)案例
- springboot整合webservice使用簡單案例總結(jié)
- Java(Springboot)項目調(diào)用第三方WebService接口實(shí)現(xiàn)代碼
相關(guān)文章
Springboot項目的服務(wù)器部署與發(fā)布方式
本文記錄了將Springboot項目部署到服務(wù)器并發(fā)布的過程,包括在IDEA中打包、選擇服務(wù)器、連接服務(wù)器、安裝環(huán)境、上傳jar包、配置環(huán)境變量以及運(yùn)行項目等步驟2025-03-03使用Java實(shí)現(xiàn)創(chuàng)建Excel表單控件
在數(shù)據(jù)填報時,創(chuàng)建Excel表單控件是一項常見的任務(wù),它可以極大地簡化數(shù)據(jù)收集和處理的過程,本文主要介紹了如何使用Java實(shí)現(xiàn)創(chuàng)建Excel表單控件,感興趣的可以了解下2024-03-03如何在springMVC的controller中獲取request
這篇文章主要介紹了如何在springMVC的controller中獲取request,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12Java設(shè)計模式以虹貓藍(lán)兔的故事講解適配器模式
適配器模式(Adapter?Pattern)是作為兩個不兼容的接口之間的橋梁。這種類型的設(shè)計模式屬于結(jié)構(gòu)型模式,它結(jié)合了兩個獨(dú)立接口的功能2022-04-04Spring-retry實(shí)現(xiàn)循環(huán)重試功能
這篇文章主要介紹了Spring-retry 優(yōu)雅的實(shí)現(xiàn)循環(huán)重試功能,通過@Retryable注解,優(yōu)雅的實(shí)現(xiàn)循環(huán)重試功能,需要的朋友可以參考下2023-07-07