前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >java webservice asmx_WebService-asmx后缀接口调用[通俗易懂]

java webservice asmx_WebService-asmx后缀接口调用[通俗易懂]

作者头像
全栈程序员站长
发布2022-07-01 12:45:39
发布2022-07-01 12:45:39
2.4K0
举报

大家好,又见面了,我是你们的朋友全栈君。

简述:.asmx是webservice服务程序的后缀名,ASP.NET 使用.asmx 文件来对Web Services的支持。.asmx 文件和.aspx文件一样都属于文本文件。它包含在.aspx文件之中,成为ASP.NET应用程序的一部分。

废话不多说上代码

POM引用

commons-discovery

commons-discovery

0.2

webService接口调用,并解析

@Override

public List> selectStoreList(String FTY_CODE_, String DEPT_CODE_)throwsException {

List> 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 = neworg.apache.axis.client.Service();

//创建一个服务(service)调用(call)

Call call = (Call) service.createCall();//通过service创建call对象

//设置service所在URL

call.setTargetEndpointAddress(endpoint);

call.setOperationName(newQName(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(newObject[]{FTY_CODE_, DEPT_CODE_});

if (xmlStr != null) {

StoreItemList storeItemList = BaseUtils.xmlToBean(xmlStr, StoreItemList.class);

MapstoreItem_;

for(StoreItem item : storeItemList.getStoreItemList()) {

storeItem_ = new HashMap();

storeItem_.put(“STORE_ID_”, item.getStoreId());

storeItem_.put(“STORE_DESC_”, item.getStoreDesc());

storeItemList_.add(storeItem_);

}

}

returnstoreItemList_;

}

以上是调用,如果有兴趣请往下看,具体实现的栗子

首先调用webServcie返回的xml数据样式要知道:

根据返回的xml文件,创建自己的StoreItemList实体类(解析xml用)

packageorg.building.er.bean;importjavax.xml.bind.annotation.XmlAccessType;importjavax.xml.bind.annotation.XmlAccessorType;importjavax.xml.bind.annotation.XmlElement;importjavax.xml.bind.annotation.XmlRootElement;importjava.io.Serializable;importjava.util.List;/***@authordimo

* @date 2020/6/28*/@XmlRootElement(name= “items”)

@XmlAccessorType(XmlAccessType.FIELD)public class StoreItemList implementsSerializable {private static final long serialVersionUID = 1L;

@XmlElement(name= “item”)private ListstoreItemList;public StoreItemList(ListstoreItemList) {this.storeItemList =storeItemList;

}public ListgetStoreItemList() {returnstoreItemList;

}public void setItemList(ListstoreItemList) {this.storeItemList =storeItemList;

}

}

根据返回的xml文件,创建自己的创建StoreItem 实体类(解析xml用)

packageorg.building.er.bean;import javax.xml.bind.annotation.*;importjava.io.Serializable;/***@authordimo

* @date 2020/6/28*/@XmlRootElement(name=”item”)

@XmlType(propOrder= {“store_id”, “store_desc”})

@XmlAccessorType(XmlAccessType.FIELD)public class StoreItem implementsSerializable {private static final long serialVersionUID = 1L;privateString store_id;privateString store_desc;publicStoreItem() {

}publicString getStoreId() {returnstore_id;

}public voidsetStoreId(String store_id) {this.store_id =store_id;

}publicString getStoreDesc() {returnstore_desc;

}public voidsetStoreDesc(String store_desc) {this.store_desc =store_desc;

}

}

说明:这里创建两个实体类的原因,返回的xml文件套了两层(即和),其解析xml要的是和的值,解析的时候转换成实体类要一层一层;

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/131398.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档