微信小程序訪問webservice(wsdl)+ axis2發布服務端(Java)


0、主要思路:使用axis2發布webservice服務端,微信小程序作為客戶端訪問。步驟如下:

1、服務端:

     首先微信小程序僅支持訪問https的url,且必須是已備案域名。因此前期的服務器端工作需要先做好,本人是申請了個人域名(已備案),並使用阿里雲服務器,然后申請免費SSL,通過配置tomcat完成支持https訪問。此外,intellJ IDE的java編譯器版本調整到8以上。

     下面進入正題:

pom.xml添加:

     <!--Axis發布webservice-->
        <!--servlet依賴-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!--服務端-->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-http</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-local</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.4.0</version>
        </dependency>
        <!--axis2 客戶端-->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <version>1.6.2</version>
        </dependency>

 

在webapp下的WEB-INF下新建如下目錄:

 

 

其中axis2.xml文件鏈接:https://files.cnblogs.com/files/qianyou304/axis2.xml

在里面添加,以支持https訪問:

<transportReceiver name="https" class="org.apache.axis2.transport.http.AxisServletListener">
    <parameter name="port">443</parameter>
</transportReceiver>

services.xml代碼:

<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
    <!-- 指定服務名,隨便定義 -->
    <service name="xxx" >
        <!-- 服務的作用說明,可寫可不寫 -->
        <description>webservice服務</description>
        <!-- 指定要發布的類路徑  自定義name-->
        <parameter name="ServiceClass">com.cn.hnust.webservice.server.xxxx</parameter>
        <!-- 類里面的方法名 ,若有多個方法,可以新增operation標簽 -->
        <operation name="xxxx">
            <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
        </operation>
    </service>
</serviceGroup>

 指定發布類,這里隨便給一個名字:ILockService,隨便取一個方法名:invoke

import com.cn.hnust.service.IUserRecordService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.web.context.support.WebApplicationContextUtils;
/**
 * 
 */
public class ILockService implements ServletContextListener{
    /** 
   *
留作備用
   */
static IUserRecordService userRecordService; @Override public void contextInitialized(ServletContextEvent sce) { userRecordService = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(IUserRecordService.class); } @Override public void contextDestroyed(ServletContextEvent sce) { } public String invoke(String xmlData) { System.out.println("xml: "+xmlData); String result = ""; JSONObject jsonObject = new JSONObject(); jsonObject = JSONObject.fromObject(xmlData); String name_method = jsonObject.getString("Name"); JSONObject infoObj = jsonObject.getJSONObject("Info"); System.out.println("name_method: "+name_method); switch (name_method) { case "LOGIN"://注冊 result = login(infoObj); break; default: break; } return result; } /** * * @param infoObj * @return */ private String login(JSONObject infoObj){ String result = ""; //解析信息字段 String userName = infoObj.getString("userName"); String password = infoObj.getString("password"); String accessTime = infoObj.getString("accessTime");//Info JSONObject info = new JSONObject(); info.put("result", "Succ"); info.put("accessType", "3"); info.put("operator", "0"); //DeviceList JSONArray deviceIDList = new JSONArray(); JSONObject Id1 = new JSONObject(); Id1.put("ID","000001"); JSONObject Id2 = new JSONObject(); Id2.put("ID","000002"); deviceIDList.add(Id1); deviceIDList.add(Id2); info.put("deviceIDList", deviceIDList); //合並 JSONObject response = new JSONObject(); response.put("Name","LOGIN_ACK"); response.put("Info", info); JSONObject resultObj = new JSONObject(); resultObj.put("Response", response); String jsonStr = resultObj.toString(); //result = XmlJsonUtil.json2xml(jsonStr); result = jsonStr; System.out.print("result: "+result); return result; } }

好了,接下來是客戶端:

在微信小程序的js中通過如下代碼調用:

    var loginJsonStr = '';   
    //method中設置你想調用的方法名
    var method = 'invoke';
    //wsdlurl中設置需要訪問的webservice的url地址
    var wsdlurl = 'https://(域名)/(項目名)/services/ILockService?wsdl';
    var targetNamespace = '(命名空間,可以去網頁輸入wsdlurl中查找)';
    //datacopy中拼字符串,即http傳輸中的soap信息
    var datacopy = '<?xml version="1.0" encoding="utf-8"?>';
    datacopy += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="(命名空間)>';
    datacopy += '<soapenv:Header/>';
    datacopy += '<soapenv:Body>';
    //接着拼你要訪問的方法名、參數名和你傳遞的實參值,比如我要訪問的方法是invoke(String arg0)
    //而我的實際調用是invoke('libsys',2),所以拼字符串如下
    datacopy += '<ser:invoke>';
    datacopy += '<xmlData>' + loginJsonStr+'</xmlData>';
    datacopy += '</ser:invoke>';
    datacopy += '</soapenv:Body>';
    datacopy += '</soapenv:Envelope>';
    wx.request({
      url: wsdlurl,
      data: datacopy,
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT 
      header: {
        'content-type': 'text/xml;',
        'SOAPAction': targetNamespace + method,
      },
      // 設置請求的 header
      success: function (res) {
        // success
        var resData = res.data;
        console.log("success:  "+resData);
      },
      fail: function (res) {
        console.log("fail:  " + res.data)
      }
    })

 


免責聲明!

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



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