WebService-asmx后綴接口調用


簡述:.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>的值,解析的時候轉換成實體類要一層一層;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM