WebService soap報文請求返回xml格式以及自定義soap模板

需求

上篇文章主要講了一個springboot集成webservice的例子,這次我們根據已經做好的webservice服務端,做一個soap接口請求,要求請求以soap報文方式請求,返回值爲xml格式

這是上篇:springboot集成webservice以及遇到的問題

soap請求接口實現

這是請求報文以及響應的報文的信息

 

 

soap接口所需要的參數信息:

soap接口代碼如下:

public static void main(String[] args) {
    String postUrl="http://localhost:8888//services/user?wsdl";//webservicve接口地址
    String soapAction="";//"http://tempuri.org/execute";
    String namespace="http://webservice.zjqf.com/";//命名空間
    int paramsNum=0;//參數個數
    String[] params={};//參數組
    String result=SoapUtil.SplicingMessage(namespace, postUrl, soapAction, "getAllData", paramsNum, params);//訪問接口
    System.out.println("返回的數據爲:"+result);
}

工具類soapUtil

 /**
     * 訪問webservice接口
     * @param namespace 命名空間
     * @param postUrl webservice接口地址
     * @param soapAction soapAction地址
     * @param method 方法名
     * @param paramsNum 參數個數
     * @param params 參數組
     * @return  返回值
     */
    public static String SplicingMessage(String namespace,String postUrl,String soapAction,String method,int paramsNum,String[] params){
        //自定義soap報文模板 注意:xmlns:web 與<soapenv:Body>後的屬性(web)要一致
        StringBuffer sb=new StringBuffer("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"");
        sb.append(namespace);
        sb.append("\"><soapenv:Header/><soapenv:Body><web:");
        sb.append(method);
        sb.append(">");
        for (int i = 0; i < paramsNum; i++) {
            sb.append("<arg0>"+params[i]+"</arg0>");
        }
        sb.append("</web:");
        sb.append(method);
        sb.append("></soapenv:Body></soapenv:Envelope>");
        String result=doPostSoap1_1(postUrl, sb.toString(), soapAction);
//        int start = result.indexOf("<return>")+8;
//        int end = result.indexOf("</return>");
//        return result.substring(start,end);
        return result;
    }
/**
 * 使用SOAP1.1發送消息
 *
 * @param postUrl
 * @param soapXml
 * @param soapAction
 * @return
 */
public static String doPostSoap1_1(String postUrl, String soapXml,
                                   String soapAction) {
    String retStr = "";
    // 創建HttpClientBuilder
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    // HttpClient
    CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
    HttpPost httpPost = new HttpPost(postUrl);
    // 設置請求和傳輸超時時間
    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(socketTimeout)
            .setConnectTimeout(connectTimeout).build();
    httpPost.setConfig(requestConfig);
    try {
        httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
        httpPost.setHeader("SOAPAction", soapAction);
        StringEntity data = new StringEntity(soapXml,
                Charset.forName("UTF-8"));
        httpPost.setEntity(data);
        CloseableHttpResponse response = closeableHttpClient
                .execute(httpPost);
        HttpEntity httpEntity = response.getEntity();
        if (httpEntity != null) {
            // 打印響應內容
            retStr = EntityUtils.toString(httpEntity, "UTF-8");
        }
        // 釋放資源
        closeableHttpClient.close();
    } catch (Exception e) {
    }
    return retStr;
}

返回xml信息:

 

題外

以下純屬個人理解,如果有誤,感謝大家指正

  1. 如果要返回xml格式的話,一定要將Content-Type設置爲xml格式;

    httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); 如果要返回json格式,需變爲json

  2. 自定義soap模板時,此處不適用含有@WebMethod修飾的方法 因爲他需要指定的參數名字,而這裏使用的都是arg0

    for (int i = 0; i < paramsNum; i++) {
                sb.append("<arg0>"+params[i]+"</arg0>");
            }

另外,對於處理這些soap報文請求時,推薦大家一款接口測試神器soapUi 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章