webservice cxf使用

环境:jdk1.7      grails2.4.0       ubuntu13

所需要的jar包:cxf-2.0.1

服务器端:

定义接口类:

public interface IApp{

  ...........//接口方法

}

定义实现类:

public class AppService implements IApp{

  //定义远程调用方法使用哪种协议;rmi不可以跨防火墙;

  static expose [‘cxf‘,‘rmi‘];

  //剔除掉不需要被客户端调用的方法;

  static excludes = [方法名];

  ............//方法实现

}

项目启动完成后在浏览器上输入http://项目所在IP:port/项目名/services/app?wsdl,整个页面会生成xml文件,说明cxf发布成功;

服务器发布完成后生成客户端:

eclipse:

1.先创建一个项目;

2.单击右键--> new -->other -->web Service client --> next ,输入http://项目所在IP:port/项目名/services/app?wsdl,生成客户端;

3.配置wsdl2java环境,window命令行输出:

//-p:wsdl的命名空间,也就是所谓的包名;  -d:指定要生成代码所在的路径;

E:\code\aa\code>wsdl2java -p com.boful.nts.wsdl.client -d . -all  http://192.168.1
.117:8080/nts/services/app?wsdl
生产完成后使用eclipse导出工具生产jar包;
4.把生成的jar包放在服务器项目中;
5.需要远程调用的时候执行下班方法:
AppService appService = new AppService(new URL(http://访问IP:port/项目名/services/app?wsdl));
AppServicePortType portType = appService.getAppServicePort();
portType调用远程方法;
//调用的方法是客户端自己生成的,所以在进行对象的增删改查的时候和服务器端接口的对象所在的包是有所区别的,就需要进行两个对象之间相互转换赋值;
 
简单两个类之间相互转换代码:

package com.boful.nts;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

public class BofulClassUtils {

    public static void cloneObject(Object src, Object dest, String[] ignored) {
        Class<?> class1 = src.getClass();
        Class<?> class2 = dest.getClass();
        Field[] fields1 = class1.getDeclaredFields();
        Field[] fields2 = class2.getDeclaredFields();
        Method[] methods1 = class1.getDeclaredMethods();
        Method[] methods2 = class2.getDeclaredMethods();
        System.out.println(fields1.length);
        for (Field field : fields1) {

            String fieldName1 = field.getName();
            if (ignored != null && ignored.length > 0) {
                if (Arrays.binarySearch(ignored, fieldName1) < 0) {
                    continue;
                }
            }


            boolean isContain = false;
            for (Field field2 : fields2) {
                if (field2.getName().equals(fieldName1)) {
                    isContain = true;
                    break;
                }
            }

            if (isContain) {
                isContain = false;
                String setMethodName = "set"
                        + fieldName1.substring(0, 1).toUpperCase()
                        + fieldName1.substring(1);
                String getMethod = "get";
                if((field.getType()==Boolean.class)||(field.getType()==boolean.class)){
                    getMethod = "is";
                }
                String getMethodName = getMethod + fieldName1.substring(0, 1).toUpperCase()
                        + fieldName1.substring(1);

                System.out.println(setMethodName);
                for (Method method : methods2) {

                    if (method.getName().equals(setMethodName)) {
                        if (method.getReturnType() == void.class) {
                            if (method.getModifiers() == Modifier.PUBLIC) {
                                Class<?>[] paramTypes = method
                                        .getParameterTypes();
                                System.out.println("--------------------" + field.getDeclaringClass().getName());
                                if ((paramTypes.length == 1)
                                        && (paramTypes[0] == field.getType())) {
                                    try {
                                        Method getMehod = class1.getMethod(getMethodName);
                                        Object value = getMehod.invoke(src);
                                        Method method2 = class2
                                                .getMethod(setMethodName,
                                                        field.getType());
                                        method2.invoke(dest, value);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public static void cloneObject(Object src, Object dest) {
        cloneObject(src, dest, null);
    }
}

 

webservice cxf使用,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。