webservice cxf使用

來源:互聯網
上載者:User

標籤: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);
    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.