xfire中对集合的配置

来源:互联网 发布:中译俄在线翻译软件 编辑:程序博客网 时间:2024/05/19 12:25

对xfire的各种方法映射和集合映射做一个总结,jar包就是xfire下面的所有。

User类:

Java代码 复制代码 收藏代码
  1. package org.forever.entity;   
  2.   
  3. import java.io.Serializable;   
  4.   
  5. public class User implements Serializable {   
  6.     private static final long serialVersionUID = 1668097150623622102L;   
  7.     private Integer id;   
  8.     private String username;   
  9.     private String password;   
  10.   
  11.     public User() {   
  12.     }   
  13.   
  14.     public User(Integer id, String username, String password) {   
  15.         super();   
  16.         this.id = id;   
  17.         this.username = username;   
  18.         this.password = password;   
  19.     }   
  20.   
  21.     public Integer getId() {   
  22.         return id;   
  23.     }   
  24.   
  25.     public void setId(Integer id) {   
  26.         this.id = id;   
  27.     }   
  28.   
  29.     public String getUsername() {   
  30.         return username;   
  31.     }   
  32.   
  33.     public void setUsername(String username) {   
  34.         this.username = username;   
  35.     }   
  36.   
  37.     public String getPassword() {   
  38.         return password;   
  39.     }   
  40.   
  41.     public void setPassword(String password) {   
  42.         this.password = password;   
  43.     }   
  44.   
  45. }  

 

PageInfo类:

Java代码 复制代码 收藏代码
  1. package org.forever.entity;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.HashMap;   
  5. import java.util.HashSet;   
  6. import java.util.List;   
  7. import java.util.Map;   
  8. import java.util.Set;   
  9.   
  10. /**  
  11.  * 说明各种集合类型的映射  
  12.  *   
  13.  * @author Administrator  
  14.  *   
  15.  */  
  16. public class PageInfo {   
  17.   
  18.     private int pageIndex = 0;   
  19.     private List<User> list = new ArrayList<User>();   
  20.     private Set<User> set = new HashSet<User>();   
  21.     private Map<String, User> map = new HashMap<String, User>();   
  22.     private User user = new User();   
  23.   
  24.     public PageInfo() {   
  25.   
  26.     }   
  27.   
  28.     public int getPageIndex() {   
  29.         return pageIndex;   
  30.     }   
  31.   
  32.     public void setPageIndex(int pageIndex) {   
  33.         this.pageIndex = pageIndex;   
  34.     }   
  35.   
  36.     public List<User> getList() {   
  37.         return list;   
  38.     }   
  39.   
  40.     public void setList(List<User> list) {   
  41.         this.list = list;   
  42.     }   
  43.   
  44.     public Set<User> getSet() {   
  45.         return set;   
  46.     }   
  47.   
  48.     public void setSet(Set<User> set) {   
  49.         this.set = set;   
  50.     }   
  51.   
  52.     public Map<String, User> getMap() {   
  53.         return map;   
  54.     }   
  55.   
  56.     public void setMap(Map<String, User> map) {   
  57.         this.map = map;   
  58.     }   
  59.   
  60.     public User getUser() {   
  61.         return user;   
  62.     }   
  63.   
  64.     public void setUser(User user) {   
  65.         this.user = user;   
  66.     }   
  67.   
  68. }  

 

PageInfo.aegis.xml映射配置文件:

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <mappings xmlns:my="http://my.bjvsp.tongtech.com">  
  3.     <mapping name="PageInfo">  
  4.         <property name="list"  
  5.             componentType="org.forever.entity.User" />  
  6.     </mapping>  
  7.     <mapping name="PageInfo">  
  8.         <property name="set" componentType="org.forever.entity.User"></property>  
  9.     </mapping>  
  10.     <mapping name="PageInfo">  
  11.         <property name="map" keyType="java.lang.String" componentType="org.forever.entity.User"></property>  
  12.     </mapping>  
  13. </mappings>  

 接口IHelloService.java:

Java代码 复制代码 收藏代码
  1. package org.forever.webservice;   
  2.   
  3. import java.util.List;   
  4. import java.util.Map;   
  5. import java.util.Set;   
  6.   
  7. import org.forever.entity.PageInfo;   
  8. import org.forever.entity.User;   
  9.   
  10. public interface IHelloService {   
  11.        
  12.     public String sayHello(String content);   
  13.     public User get(User user);   
  14.     public List getAll(List list);   
  15.        
  16.     public PageInfo getPage(User user,List list,Set set,Map map);   
  17.        
  18.        
  19. }  

 

接口实现:

Java代码 复制代码 收藏代码
  1. package org.forever.webservice.impl;   
  2.   
  3. import java.util.List;   
  4. import java.util.Map;   
  5. import java.util.Set;   
  6.   
  7. import org.forever.entity.PageInfo;   
  8. import org.forever.entity.User;   
  9. import org.forever.webservice.IHelloService;   
  10.   
  11. public class HelloServiceImpl implements IHelloService {   
  12.   
  13.     public User get(User user) {   
  14.         System.out.println("HelloServiceImpl.get(user);");   
  15.         user.setUsername("sotry");   
  16.         return user;   
  17.     }   
  18.   
  19.     public List getAll(List list) {   
  20.         System.out.println("HelloServiceImpl.getAll(List list);");   
  21.         list.add(new User(88"唐平""admin"));   
  22.         list.add(new User(99"王维""forever"));   
  23.         return list;   
  24.     }   
  25.   
  26.     public String sayHello(String content) {   
  27.         System.out.println("HelloServiceImpl.sayHello(String content);");   
  28.         return content+"哦也";   
  29.     }   
  30.   
  31.        
  32.     public PageInfo getPage(User user, List list, Set set, Map map) {   
  33.         System.out.println("HelloServiceImpl.getPage()");   
  34.         user.setUsername("webService update username");   
  35.         list.add(new User(3"admin""admin"));   
  36.         set.add(new User(3"admin""admin"));   
  37.         map.put("key"new User(3"admin""admin"));   
  38.         PageInfo pageInfo = new PageInfo();   
  39.         pageInfo.setList(list);   
  40.         pageInfo.setSet(set);   
  41.         pageInfo.setMap(map);   
  42.         pageInfo.setUser(user);   
  43.         return pageInfo;   
  44.     }   
  45.   
  46. }  

 

IHelloService.aegis.xml映射:

Xml代码 复制代码 收藏代码
  1. <mappings  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://xfire.codehaus.org/schemas/1.0/mapping.xsd">  
  3.     <mapping>  
  4.          <method name="getAll">  
  5.             <parameter index="0" componentType="org.forever.entity.User"/>  
  6.             <return-type mappedName="userList" componentType="org.forever.entity.User" />  
  7.         </method>  
  8.            
  9.         <method name="getPage">  
  10.             <parameter index="1" componentType="org.forever.entity.User"/>  
  11.             <parameter index="2" componentType="org.forever.entity.User"/>  
  12.             <!-- map映射失败 -->  
  13.             <parameter index="3" mappedName="userMap" keyType="java.lang.String" componentType="org.forever.entity.User"/>  
  14.             <return-type componentType="org.forever.entity.PageInfo" />  
  15.         </method>    
  16.    </mapping>  
  17.       
  18.       
  19. </mappings>     

 

IHelloService.doc.xml应该可以不用写吧:

Xml代码 复制代码 收藏代码
  1. <service>  
  2.   <documentation>  
  3.     Service which allows to search for books   
  4.   </documentation>  
  5.   <method name="get" parametersNumber="1" >  
  6.      <documentation>  
  7.        根据所给的用户,填充名字   
  8.      </documentation>  
  9.      <parameter index="0">  
  10.         <documentation>  
  11.            用户实体   
  12.         </documentation>  
  13.         </parameter>  
  14.            
  15.       <return>  
  16.         <documentation>  
  17.          带名字的实体   
  18.         </documentation>  
  19.       </return>  
  20.     
  21.          
  22.   </method>  
  23.      
  24.     <method name="getAll" parametersNumber="1" >  
  25.         <documentation>  
  26.       继续将传递过来的list进行填充   
  27.      </documentation>  
  28.      <parameter index="0">  
  29.         <documentation>  
  30.           用户集合   
  31.         </documentation>  
  32.         </parameter>  
  33.            
  34.       <return>  
  35.         <documentation>  
  36.          用户集合   
  37.         </documentation>  
  38.       </return>  
  39.   </method>  
  40.      
  41.   
  42. </service>  

 客户端代码HelloClient类:

Java代码 复制代码 收藏代码
  1. import java.net.MalformedURLException;   
  2. import java.net.URL;   
  3. import java.util.ArrayList;   
  4. import java.util.HashMap;   
  5. import java.util.HashSet;   
  6. import java.util.List;   
  7.   
  8. import org.codehaus.xfire.client.Client;   
  9. import org.codehaus.xfire.client.XFireProxyFactory;   
  10. import org.codehaus.xfire.service.Service;   
  11. import org.codehaus.xfire.service.binding.ObjectServiceFactory;   
  12. import org.forever.entity.PageInfo;   
  13. import org.forever.entity.User;   
  14. import org.forever.webservice.IHelloService;   
  15.   
  16.   
  17. public class HelloClient {   
  18.   
  19.     public static void main(String[] args) {   
  20.            
  21.         //创建服务   
  22.         Service service = new ObjectServiceFactory().create(IHelloService.class);   
  23.         //创建代理工厂   
  24.         XFireProxyFactory fireProxyFactory = new XFireProxyFactory();   
  25.         //服务地址   
  26.         try {   
  27.             IHelloService helloService = (IHelloService) fireProxyFactory.create(service, "http://127.0.0.1:8080/WebService/services/helloService");   
  28.             System.out.println(helloService.sayHello("我是神话"));   
  29.             User user = new User(1,"wrwr","wrwrwr");   
  30.             helloService.get(user);   
  31.             System.out.println(user.getUsername());   
  32.                
  33.             List<User> users = new ArrayList<User>();   
  34.             users.add(new User(1,"wrwr","wrwrwr"));   
  35.             users.add(new User(1,"wrwr","wrwrwr"));   
  36.             users = helloService.getAll(users);   
  37.             System.out.println(users.size());   
  38.             System.out.println("***************************");   
  39.             PageInfo pageInfo = helloService.getPage(new User(), new ArrayList(),new HashSet<User>(),new HashMap<String, User>());   
  40.             System.out.println("客户端交互完成");   
  41.         } catch (MalformedURLException e) {   
  42.             e.printStackTrace();   
  43.         }   
  44.            
  45.         System.out.println("使用client交互:");   
  46.         Object[] results;   
  47.         try {   
  48.             Client client = new Client(new URL(   
  49.             "http://127.0.0.1:8080/WebService/services/helloService?wsdl"));   
  50.             results = client   
  51.                     .invoke("sayHello"new Object[] {"我来了哈"});   
  52.             System.out.println(results[0]);   
  53.         } catch (MalformedURLException e) {   
  54.             e.printStackTrace();   
  55.         } catch (Exception e) {   
  56.             e.printStackTrace();   
  57.         }   
  58.            
  59.            
  60.            
  61.     }   
  62. }  

 

 

在运行客户端代码一定要引入业务接口的包,如果在一个项目里面就可以省略。