当前位置:   article > 正文

web Service 工具类_命令创建webservice工具类

命令创建webservice工具类
package com.kBox.util;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * call调用webservice接口用具类
 * Created by AlterEgo on 2018/6/7.
 */
public class CallWebServiceUtils {
 
    /**
     * call调用webservice接口
     * @param url 接口地址
     * @param nameSpace 命名空间
     * @param methodName 方法名
     * @param paramMap 参数Map
     * @return
     */
    public static OMElement callWebServiceAxis2(String url, String nameSpace, String methodName, Map<String, Object> paramMap) throws Exception {
        OMElement result = null;
        try {
            ServiceClient serviceClient = new ServiceClient();
            //创建WebService的URL
            EndpointReference targetEPR = new EndpointReference(url);
            Options options = serviceClient.getOptions();
            options.setTo(targetEPR);
            //确定调用方法( 命名空间地址 (namespace) 和 方法名称)
            options.setAction(nameSpace + methodName);
            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMNamespace omNs = fac.createOMNamespace(nameSpace, "");
            OMElement method = fac.createOMElement(methodName, omNs);
            // 遍历传入方法的参数
            for (String key : paramMap.keySet()) {
                OMElement element = fac.createOMElement(key, omNs);
                Object obj = paramMap.get(key);
                if (obj != null) {
                    element.setText(paramMap.get(key).toString());
                }
                method.addChild(element);
            }
            method.build();
            //调用接口
            result = serviceClient.sendReceive(method);

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        }
        return result;
    }
 
    /**
     * call调用webservice接口
     * @param url 接口地址
     * @param nameSpace 命名空间
     * @param methodName 方法名
     * @param paramMap 参数Map
     * @return
     */
    public static String callWebServiceAxis2Text(String url, String nameSpace, String methodName, Map<String, Object> paramMap) throws Exception {
        OMElement element = callWebServiceAxis2(url, nameSpace, methodName, paramMap);
        String resultTexe = "";
        if (element != null) {
            resultTexe = element.getFirstElement().getText();
        }
        return resultTexe;
    }

    /**
     * 解析xml文件返回保存cls的List集合,如果返回码resultCode=1时则返回List为null
     *
     * @param xml
     * @param cls
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static List<?> parseXml2List(String xml, Class<?> cls)
            throws Exception {
        List<Object> lists = null;
        Document doc = DocumentHelper.parseText(xml);
        Element et = doc.getRootElement();
        String root = et.getName();
        // 查看返回码是否为真.
        List<Element> list = doc.selectNodes("//" + root + "/resultCode");
        if (!list.isEmpty() && list.size() > 0) {
            Element element = list.get(0);
            String returnResult = element.getText();
            if (returnResult.equals("0")) {
                List<Element> father = doc.selectNodes("//" + root + "/"+ cls.getSimpleName()+"s");
                //判断对象父节点是否有包含数据
                if(father!=null&&!father.isEmpty()&&father.size()==1){
                    List<Element> userLists = father.get(0).elements();
                    if (userLists!=null&&!list.isEmpty()) {
                        lists = new ArrayList<Object>();
                        for (Element e : userLists) {
                            List<Element> li = e.elements();
                            Class<?> cl = (Class<?>) Class.forName(cls.getName());
                            Object ob = cl.newInstance();
                            for (Element element2 : li) {
                                String name = element2.getName();
                                String value = element2.getText();
                                Field field = ob.getClass().getDeclaredField(name);
                                field.setAccessible(true);
                                convertValue(field, ob, value);
                            }
                            lists.add(ob);
                        }
                    }
                }
            }

        }
        return lists;
    }
    /**
     * 反射设置实体不同类型字段的值 <暂时只支持 日期 字符串 boolean Integer值设置 待扩建>
     *
     * @param field
     * @param obj
     * @param value
     * @throws Exception
     */
    public static void convertValue(Field field, Object obj, String value)
            throws Exception {
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (field.getGenericType().toString().equals("class java.lang.Integer")) {
            field.set(obj, Integer.parseInt(value));
        } else if (field.getGenericType().toString().equals("boolean")) {
            field.set(obj, Boolean.parseBoolean(value));
        } else if (field.getGenericType().toString().equals(
                "class java.util.Date")) {
            field.set(obj, sim.parse(value));
        } else {
            field.set(obj, value);
        }

    }
}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/187013
推荐阅读
相关标签
  

闽ICP备14008679号