Xfire封裝對象和List型對象


現在學習需要,學習了WebServices,實用了框架是Xfire,可能最新的是CXF和axis2,但是着手的項目是Xfire的框架,沒辦法,今天學習瞭如何對對象進行封裝和調用。

對對象的封裝參考了http://blog.csdn.net/daryl715/article/details/1704981下的一篇博文,但調用的時候着實自己修改了下。

 

1...............................首先創建Service端,前面的文章也詳細的介紹了Xfire如何創建服務器端,大家可以參考下

先看看服務器端結構圖吧

 

1.0..........................User.java

package com.ListBinding;

public class User {
   private String username;
   private String password;
   public User(){
       
   }
   public User(String username,String password){
       this.username=username;
       this.password=password;
       
   }
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
}

1.1..............................IHelloWorldService.java

package com.ListBinding;

import java.util.List;

public interface IHelloWorldService {
  public User HelloWorld(User user); //傳入和返回自定義類型
  public List HelloWorldList(); //返回集合類型
}


1.2.........................HelloWorldServiceImpl.java

package com.ListBinding;

import java.util.ArrayList;
import java.util.List;
public class HelloWorldServiceImpl implements IHelloWorldService {

    public User HelloWorld(User user) {
      if(user.getUsername().equals("gaoxiang")){
          return new User("welcome gaoxiang","1234");
      }
      else{
          return new User("wrong name","1234");
      }
    
    }

    public List HelloWorldList() {
        
        List a=new ArrayList();
        User u1=new User("1","1");
        User u2=new User("2","2");
        User u3=new User("3","3");
        a.add(u1);
        a.add(u2);
        a.add(u3);
        return a;
    }

}


1.3............................實用默認的綁定方式ageis,所以必須要在相同目錄下創建IHelloWorldService.aegis.xml文件,切記接口名稱和這了的前面名稱必須相同

 

<?xml version="1.0" encoding="UTF-8"?>
<mappings>
    <mapping>
        <method name="HelloWorld">
            <parameter index="0" componentType="com.ListBinding.User"/>
        </method>
  
        <method name="HelloWorld">
            <return-type componentType="com.ListBinding.User"/>
        </method>
        <method name="HelloWorldList">
            <return-type componentType="com.ListBinding.User"/>  <!-- 定義返回集合類型中元素的type -->
        </method>
    </mapping>
</mappings>

1.4.........................在Service.xml中進行配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">

	<service>
		<name>HelloService</name>
		<serviceClass>com.ListBinding.IHelloWorldService</serviceClass>
		<implementationClass>com.ListBinding.HelloWorldServiceImpl
		</implementationClass>
		<style>wrapped</style>
		<use>literal</use>
		<scope>application</scope>
	</service>
</beans>

1.5........................配置好進行發佈

在如下地址進行查看http://localhost:8080/ServicesBinding/services/HelloService?wsdl發佈後的wsdl文件

 

當然也可以現在MyEclipse中的Web Services Explorer中進行測試,我的測試結果圖如下:

好了 服務器端我們完成了,下面就來開發客戶端了,來體驗一下我們的發佈成果,呵呵

 

 

2.....................................客戶端我分別實用Eclipse和MyEclipse進行了開發,來一起看看吧。

 

首先Eclipse進行調用服務接口。

2..1................................首先新建一個Java工程,這裏我沒有用Web工程,其實結果都是一樣的,

                                     然後右擊src木了,新建一個Web Service client 如下圖

 

2.2...........................點擊next之後,會讓你輸入服務端口地址,如下所示()也就是WSDL的地址:

2.3...........................................完成之後直接點擊Finish就行了,這樣我們計引用了服務端口,系統能夠回自動幫我們創建文件如下:

這是客戶端工程結構圖,其中Test包是自己創建的,系統自動創建的是ListBinding包。

2.4....................................下面我們編寫客戶端測試代碼:

client.java

package com.Test;



import java.rmi.RemoteException;

import com.ListBinding.HelloServicePortTypeProxy;
import com.ListBinding.User;

public class client {
	public static void main(String[] args) throws RemoteException {
		HelloServicePortTypeProxy client =  new HelloServicePortTypeProxy();
		User[] user = client.helloWorldList();
		System.out.println(user[2].getUsername());
		User info = new User();
		info.setUsername("gaoxiang");
		info.setPassword("123");
		User sa = client.helloWorld(info);
		System.out.println(sa.getUsername());
	}

}


2.5...............................運行客戶端代碼,控制檯顯示結果如下所示:

Eclipse客戶端測試成功。

 

 

 

3...................................下面看看用MyEclipse進行客戶端測試

 

3.1................................同樣首先創建一個ServicesBindingClient工程

3.2...............................在src右擊創建web Service client 客戶端(和前面相同)

 

 在這裏選擇JAX-WS創建:

3.3...............................................下一步之後輸入WSDL地址和新建一個包名,如下所示:

3.4............................................Finish,工程結構圖如下所示:

3.5...........................................同樣TestClient使我們自己創建的測試包

test.java如下所示:

package TestClient;

import java.util.List;

import Client.ArrayOfUser;
import Client.HelloService;
import Client.HelloServicePortType;
import Client.User;

public class test {

	public static void main(String[] args) {
		HelloService service = new HelloService();
		HelloServicePortType client =service.getHelloServiceHttpPort();
		ArrayOfUser user = client.helloWorldList();
		List<User> users =  user.getUser();		
		System.out.println(users.size());		
		User newuser = users.get(0);
		User sa = client.helloWorld(newuser);
		System.out.println(sa.getUsername().getValue());
		
	}
}

 

3.6......................................運行我們的測試,測試結果如下所示:

4......................................至此我們測試完成,得到成果...............完成對象和List的封裝。


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章