1. 程式人生 > >java調.net的webservice介面的用法

java調.net的webservice介面的用法

package com.fbb.ap.utils;
import javax.annotation.PostConstruct;
import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * SMS 工具類
 * @author Mark
 *
 */
@PropertySource(value = "classpath:fbbap.properties")
@Component
public class SmslUtil {
    
    private static String smsServerUrl = "http://10.0.0.1/service1.asmx";
     
    @Value("${fbbap.smsservurl}")
    private String mSmsServerUrl;
 
    @PostConstruct
    public void getWxUtil() {
        smsServerUrl = this.mSmsServerUrl;
    }
 
    public static String getFilePath() {
        // lockie.zou
        return smsServerUrl;
    }
    private static String methodName = "SendSMS";
    private static String userName = "BI";
    private static String systemName ="FUBAOBAO";
    private static final Logger log = LoggerFactory.getLogger("adminLogger");
    
    public static String sendSMS(String mobilePhone,String content) {
        String ret = "";
        Service service = new Service();
        String namespaceURL="http://tempuri.org/";
        try {
            Call call = (Call)service.createCall();  //通過service建立call物件
            call.setTargetEndpointAddress(new java.net.URL(smsServerUrl+"?wsdl")); //設定service所在URL
            call.setOperationName(new QName(namespaceURL,methodName));
            call.addParameter(new QName(namespaceURL,"userName"),org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
            call.addParameter(new QName(namespaceURL,"systemName"),org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
            call.addParameter(new QName(namespaceURL,"phonenumbers"),org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
            call.addParameter(new QName(namespaceURL,"contents"),org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
            call.setUseSOAPAction(true);
            Object[] c={userName,systemName,mobilePhone,content};
            String result = (String)call.invoke(c);  //從這一步直接跳到System.out.println(e.toString());
            System.out.println(result);
            ret = result;
            } 
        catch (Exception e) {
                //log.error("sendSMS error:" + e.toString());
                 System.out.println(e.toString());
            }
            return ret;
    }
    
}

ap層

  /**
     * 功能:基於sms簡訊介面的傳送
     * @author dev3
     * @param  sms
     * 
     * */
    @PostMapping("/send")
    @ResponseBody
    public String send(@RequestBody Sms sms) {
        String strRet="";
        String mobilePhone = sms.getMobilephone();
        String content = sms.getContent();
        smsDao.save(sms);
        strRet = SmslUtil.sendSMS(mobilePhone,content);
        return strRet;
    }

fbb層

    @PostMapping("/send")
    @ResponseBody
    public String send(@RequestBody Sms sms) {
        String mobilePhone = sms.getMobilephone();
        String content = sms.getContent();
        String requestURL="/sms/send";
        String strRet="";
        try{
            strRet = httpUtils.sendPost(requestURL, sms);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        System.out.println(strRet);
        return strRet;
    }

其中出現的問題:

org.apache.commons.discovery.tools.DiscoverSingleton

原因: 缺少 commons-discovery-0.2.jar

  1. Resolved exception caused by handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported

原因:post只支援https    get只支援http

  1. java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [META-INF/services/org.apache.axis.EngineConfigurationFactory]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.

原因:先將專案remove,然後停止伺服器。到應用程式目錄下部署的應用刪除,然後啟動伺服器,搞定。

3.在第二步基礎之上出現

System.Web.Services.Protocols.SoapException: 伺服器未能識別 HTTP SOAPAction 的值:

原因:沒有設SOAPAction的值.