码迷,mamicode.com
首页 > Web开发 > 详细

WebService + xml

时间:2016-05-20 15:59:26      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

一、请求参数

package com.zhicall.core.ws;

import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class WSRequest {
    
    private String methodName;
    
    private String wsurl;
    
    private String name;
    
    private Map<String,String> params;

    public WSRequest(){}
    
    public WSRequest(String wsurl,String name,Map<String,String> params,String methodName){
        this.wsurl = wsurl;
        this.name = name;
        this.methodName = methodName;
        this.params = params;
    }
    
    public String getWsurl() {
        return wsurl;
    }

    public void setWsurl(String wsurl) {
        this.wsurl = wsurl;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }
    
    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getRequestXml(){
        Document doc = DocumentHelper.createDocument();
        Element reqele = doc.addElement("Request");
        reqele.addElement("FunName").setText(methodName);
        Element parele = reqele.addElement("Params");
        if(params != null && !params.isEmpty()){
            for (String key : params.keySet()) {
                Element param = parele.addElement("Param");
                String value = params.get(key);
                param.addAttribute("Name", key).addAttribute("Value", value);
            }
        }
        return doc.asXML();
    }
}

二、结果获取

package com.zhicall.core.ws;

import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WebServiceUtil {

    private static Logger logger = LoggerFactory.getLogger(WebServiceUtil.class);

    public static String getWsResult(WSRequest request) {
        String result = "no result!";
        try {
            String endpoint = request.getWsurl();
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            // 服务名
            call.setOperationName(new QName("http://tempuri.org/", request.getName()));
            // 定义入口参数和参数类型
            call.addParameter("Data", XMLType.XSD_STRING, ParameterMode.IN);
            call.setUseSOAPAction(true);
            // Action地址
            // call.setSOAPActionURI("http://tempuri.org/"+"XHLisWebserviceFun");
            // 定义返回值类型
            call.setReturnType(new QName("http://www.w3.org/2001/XMLSchema-instance", "String"));

            // 调用服务获取返回值
            String requestxml = request.getRequestXml();
            logger.info("getWsResult:requestXML:"+requestxml);
            result = String.valueOf(call.invoke(new Object[] { requestxml }));
            logger.info("getWsResult:responseXML:"+result);
            return result.replace("&nbsp;", " ");
        } catch (ServiceException e) {
            logger.info("getWsResult:ERROR"+e.getMessage());
        } catch (RemoteException e) {
            logger.info("getWsResult:ERROR"+e.getMessage());
        } catch (MalformedURLException e) {
            logger.info("getWsResult:ERROR"+e.getMessage());
        }
        return result;
    }
}

三、XML解析成对象

XStream xs = new XStream();
        xs.processAnnotations(new Class[]{ResponseResult.class,LisReponse.class});
        Object obj = xs.fromXML(wsResult);

四、对象参数

package com.zhicall.domain.wsentity;

import java.util.List;

import org.apache.ibatis.type.Alias;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("Response")
public class ResponseResult<T> {

    @XStreamAlias("ResultCode")
    private String ResultCode;

    @XStreamAlias("ResultMsg")
    private String ResultMsg;

    @XStreamAlias("Items")
    private List<T> Items;

    public String getResultCode() {
        return ResultCode;
    }

    public void setResultCode(String resultCode) {
        ResultCode = resultCode;
    }

    public String getResultMsg() {
        return ResultMsg;
    }

    public void setResultMsg(String resultMsg) {
        ResultMsg = resultMsg;
    }

    public List<T> getItems() {
        return Items;
    }

    public void setItems(List<T> items) {
        Items = items;
    }

}

 

WebService + xml

标签:

原文地址:http://www.cnblogs.com/binbang/p/5512373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!