CXF--客戶端

BtWangZhi發表於2017-10-27

1 由wsdl2java工具根據服務端生成客戶端部分程式碼。工具下載位置:
http://cxf.apache.org/download.html,解壓後將檔案路徑新增到環境變數,和jdk的安裝差不多。
在服務端之外另外建立一個專案,進行如下操作:
生成部分客戶端的操作如下:
這裡寫圖片描述
生成程式碼如下:
這裡寫圖片描述
參考:http://blog.java1234.com/blog/articles/52.html

2 客戶端遠端呼叫服務端的方法。

public class Client {
    public static void main(String[] args){
        HelloServiceService helloServiceService=new HelloServiceService();
        HelloService helloService=helloServiceService.getHelloServicePort();
        System.out.println(helloService.say("德瑪。。。。"));
    }
}

啟動服務端,然後執行客戶端中的main函式,執行結果如下:
這裡寫圖片描述

3 客戶端遠端呼叫JavaBean返回值的方法。
3.1 服務端:

@WebService
public interface HelloService {
    public User getUser();
}

@WebService
public class HelloServiceImpl implements HelloService{

    public String say(String str) {
        return "Hello"+str;
    }

    public User getUser() {
        User user=new User();
        user.setId(1);
        user.setName("dema");
        user.setPassword("123");
        return user;
    }
}

客戶端呼叫:

public class Client {
    public static void main(String[] args){
        HelloServiceService helloServiceService=new HelloServiceService();
        HelloService helloService=helloServiceService.getHelloServicePort();
        //System.out.println(helloService.say("德瑪。。。。"));

        User user=helloService.getUser();
        System.out.println(user.getName());
    }
}

這裡寫圖片描述

相關文章