简述:.asmx是webservice服务程序的后缀名,ASP.NET 使用.asmx 文件来对Web Services的支持。.asmx 文件和.aspx文件一样都属于文本文件。它包含在.aspx文件之中,成为ASP.NET应用程序的一部分。
废话不多说上代码
POM引用
<dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.2</version> </dependency>
webService接口调用,并解析
@Override
public List<Map<String, Object>> selectStoreList(String FTY_CODE_, String DEPT_CODE_) throws Exception {
List<Map<String, Object>> storeItemList_ = new ArrayList<>();
//获取webservice接口地址
String endpoint = "http://10.18.26.71/WebService_SB/WS_EquipService.asmx";
//获取域名地址,server定义的
String soapaction = "http://tempuri.org/";
//调用的方法名
String method = "getStoreList";
// 创建一个服务(service)调用(call)
org.apache.axis.client.Service service = new org.apache.axis.client.Service();
// 创建一个服务(service)调用(call)
Call call = (Call) service.createCall();// 通过service创建call对象
// 设置service所在URL
call.setTargetEndpointAddress(endpoint);
call.setOperationName(new QName(soapaction, method));
//设置参数及类型,与接口参数对应
call.addParameter(new QName(soapaction, "plant"),
org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "depart"),
org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
call.setUseSOAPAction(true);
call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_STRING); //返回参数的类型
call.setSOAPActionURI(soapaction + method); //这个也要注意 就是要加上要调用的方法getStoreList,不然也会报错
//invoke调用方法并传递参数,获取XML
String xmlStr = (String) call.invoke(new Object[]{FTY_CODE_, DEPT_CODE_});
if (xmlStr != null) {
StoreItemList storeItemList = BaseUtils.xmlToBean(xmlStr, StoreItemList.class);
Map<String, Object> storeItem_;
for (StoreItem item : storeItemList.getStoreItemList()) {
storeItem_ = new HashMap<String, Object>();
storeItem_.put("STORE_ID_", item.getStoreId());
storeItem_.put("STORE_DESC_", item.getStoreDesc());
storeItemList_.add(storeItem_);
}
}
return storeItemList_;
}
以上是调用,如果有兴趣请往下看,具体实现的栗子
首先调用webServcie返回的xml数据样式要知道:
<string> <items> <item> <store_id>234</store_id> <store_desc>金矿厂机关作业区备件库房</store_desc> </item> <item> <store_id>233</store_id> <store_desc>金矿厂机关作业区材料库房</store_desc> </item> <item> <store_id>235</store_id> <store_desc>金矿厂机关作业区材料1库房</store_desc> </item> <item> <store_id>236</store_id> <store_desc>金矿厂机关作业区虚拟库房</store_desc> </item> </items> </string>
根据返回的xml文件,创建自己的StoreItemList实体类(解析xml用)
package org.building.er.bean; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import java.io.Serializable; import java.util.List; /** * @author dimo * @date 2020/6/28 */ @XmlRootElement(name = "items") @XmlAccessorType(XmlAccessType.FIELD) public class StoreItemList implements Serializable { private static final long serialVersionUID = 1L; @XmlElement(name = "item") private List<StoreItem> storeItemList; public StoreItemList(List<StoreItem> storeItemList) { this.storeItemList = storeItemList; } public List<StoreItem> getStoreItemList() { return storeItemList; } public void setItemList(List<StoreItem> storeItemList) { this.storeItemList = storeItemList; } }
根据返回的xml文件,创建自己的创建StoreItem 实体类(解析xml用)
package org.building.er.bean; import javax.xml.bind.annotation.*; import java.io.Serializable; /** * @author dimo * @date 2020/6/28 */ @XmlRootElement(name ="item") @XmlType(propOrder = {"store_id", "store_desc"}) @XmlAccessorType(XmlAccessType.FIELD) public class StoreItem implements Serializable { private static final long serialVersionUID = 1L; private String store_id; private String store_desc; public StoreItem() { } public String getStoreId() { return store_id; } public void setStoreId(String store_id) { this.store_id = store_id; } public String getStoreDesc() { return store_desc; } public void setStoreDesc(String store_desc) { this.store_desc = store_desc; } }
说明:这里创建两个实体类的原因,返回的xml文件套了两层(即<items>和<item>),其解析xml要的是<store_id>和<store_desc>的值,解析的时候转换成实体类要一层一层;