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

webservice cxf使用

时间:2014-06-30 15:31:15      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   java   color   使用   

环境: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使用,布布扣,bubuko.com

webservice cxf使用

标签:des   style   http   java   color   使用   

原文地址:http://www.cnblogs.com/lvy6/p/3816241.html

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