前言
WebService 也叫XML Web
Service,WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。
一、使用HTTP+SOAP方式远程调用
maven引入依赖:
<!--webservice 使用hutool的SoapClient -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.22</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
在线wsdl示例:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
package com.gcddd.earthquake.controller;
import cn.hutool.http.webservice.SoapClient;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class WebServiceTest {
public static void main(String[] args) {
//请求地址
String soapUrl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
HashMap<String, Object> map = new HashMap<>();
map.put("theCityName", "重庆");//请求参数,多个继续put即可
SoapClient soapClient = SoapClient.create(soapUrl)
//请求方法,命名空间
.setMethod("web:getWeatherbyCityName", "http://WebXml.com.cn/")
.setParams(map);
String sendSoap = soapClient.send(true);
System.out.println(sendSoap);
Map<String, String> map1 = new HashMap<String, String>();
Map soapMap = XmlMap(sendSoap, map1);
System.out.println(JSONObject.toJSONString(soapMap));
}
public static Map<String, String> XmlMap(String xml, Map<String, String> map) {
try {
SAXReader reader = new SAXReader();
Document doc = reader.read(new StringReader(xml));
Element root = doc.getRootElement();
String path = "";
if (map.containsKey(root.getName().trim())) {
path = map.get(root.getName().trim());
map.remove(root.getName().trim());
}
for (Iterator i = root.elementIterator(); i.hasNext(); ) {
Element element = (Element) i.next();
if (element.isTextOnly()) {
if (path.length() > 0) {
map.put(path + element.getName().trim(), element.getTextTrim());
} else {
map.put(element.getName().trim(), element.getTextTrim());
}
} else {
map.put(element.getName().trim(), path + element.getName().trim() + ".");
XmlMap(element.asXML(), map);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
运行即可看到结果:
二、IDEA生成webservice客户端
1.idea添加webservice客户端模块
由于idea版本差异较大,功能所在不一样,我用的2023.1
设置-项目结构
模块-新增模块-webservice客户端
ws引擎选择Apaache Axis,选择之后可能不会出现代码生成的页面,可以先移除模块,然后右键项目
添加框架支持
其中生成的WebServiceTest代码貌似不能用,改为这样:
package example;
import mypackage.ICEApiService_PortType;
import mypackage.ICEApiService_ServiceLocator;
import mypackage.ResultModel;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
public class HelloWorldClient {
public static void main(String[] argv) throws RemoteException, MalformedURLException {
ICEApiService_ServiceLocator iceApiServiceServiceLocator = new ICEApiService_ServiceLocator();
ICEApiService_PortType iceApiServiceImplPort = iceApiServiceServiceLocator.getICEApiServiceImplPort(new URL("http://IP:8808/ws/ice/api?wsdl"));
ResultModel resultModel = iceApiServiceImplPort.send_open_message_data_new("eHFz", "测试消息推送", "这是摘要!", "这是消息推送正文!!!", "0dsa4906", "0", "https://www.baidu.com/", "1234", "0", "123");
}
}
因为生成的lib包是外部的,需要加入进项目,或者安装到maven才能使用,右键”添加为库“
2.读入数据
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。