SpringBoot调用对方webService接口的几种方法示例
作者:小小鱼儿小小林
前言
平常我们开发调用接口一般会用到几种数据格式,比如有restful的,这个是目前最流行的,也是最简单开发的,还有一种就是webservice数据格式,这个应该是很久以前的一些项目是用的这种
那什么是webservice呢,Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序
在调用别人写好的webservice服务的时候,对方会给你一串schema文件(xsd文件)或者是wsdl结尾的地址,你访问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>
二、如何调用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生成的例子:
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,然后才能生成,这里我就不写了,我就在这里写一下要注意的一点:要引入的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调用对方webService接口的几种方法示例的详细内容,更多关于SpringBoot调用webService的资料请关注脚本之家其它相关文章!