CFX系列(二):CXF处理复杂的对象

来源:互联网 发布:中秋节淘宝有活动吗 编辑:程序博客网 时间:2024/06/06 07:32


CXF支持所有的自定义的对象以及常见的诸如List对象,可是对于Map CXF不支持,比如现在服务端根据客户端传递过来一个用户名返回一个MAP形式的权限集合,一发布就会报错,这是需要将map用适配器转换才能发布,本文将做介绍,项目框架沿用第一篇的。


服务端代码:

1:自定义一个MyRole对象

[java] view plain copy
  1. package com.qqw.entity;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class MyRole {  
  6.   
  7.     private String key;  
  8.     private List<Role> value;  
  9.     public MyRole() {  
  10.         super();  
  11.         // TODO Auto-generated constructor stub  
  12.     }  
  13.     public MyRole(String key, List<Role> value) {  
  14.         super();  
  15.         this.key = key;  
  16.         this.value = value;  
  17.     }  
  18.     public String getKey() {  
  19.         return key;  
  20.     }  
  21.     public void setKey(String key) {  
  22.         this.key = key;  
  23.     }  
  24.     public List<Role> getValue() {  
  25.         return value;  
  26.     }  
  27.     public void setValue(List<Role> value) {  
  28.         this.value = value;  
  29.     }  
  30.       
  31.       
  32. }  

2:在接口HelloWorld返回map的方法里:

[java] view plain copy
  1. package com.qqw.webservice;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import javax.jws.WebService;  
  7. import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;  
  8.   
  9. import com.qqw.entity.MapAdapter;  
  10. import com.qqw.entity.Role;  
  11. import com.qqw.entity.User;  
  12.   
  13.   
  14.   
  15. @WebService  
  16. public interface HelloWorld {  
  17.   
  18.     public String say(String str);  
  19.     public List<Role> getUserRole(User user);  
  20.       
  21.     @XmlJavaTypeAdapter(MapAdapter.class)   //采用适配器转换  
  22.     public Map<String, List<Role>> getMap();  
  23. }  


3:用户user

[java] view plain copy
  1. package com.qqw.entity;  
  2.   
  3. public class User {  
  4.   
  5.     private String name;  
  6.     private Integer id;  
  7.     public User() {  
  8.         super();  
  9.         // TODO Auto-generated constructor stub  
  10.     }  
  11.     public User(String name, Integer id) {  
  12.         super();  
  13.         this.name = name;  
  14.         this.id = id;  
  15.     }  
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.     public Integer getId() {  
  23.         return id;  
  24.     }  
  25.     public void setId(Integer id) {  
  26.         this.id = id;  
  27.     }  
  28.       
  29.       
  30. }  

4:角色role

[java] view plain copy
  1. package com.qqw.entity;  
  2.   
  3. public class Role {  
  4.   
  5.     private Integer id;  
  6.     private String name;  
  7.     public Role() {  
  8.         super();  
  9.         // TODO Auto-generated constructor stub  
  10.     }  
  11.     public Role(Integer id, String name) {  
  12.         super();  
  13.         this.id = id;  
  14.         this.name = name;  
  15.     }  
  16.     public Integer getId() {  
  17.         return id;  
  18.     }  
  19.     public void setId(Integer id) {  
  20.         this.id = id;  
  21.     }  
  22.     public String getName() {  
  23.         return name;  
  24.     }  
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28.       
  29.       
  30. }  

5:定义适配器

[java] view plain copy
  1. package com.qqw.entity;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import javax.xml.bind.annotation.adapters.XmlAdapter;  
  8.   
  9. public class MapAdapter extends XmlAdapter<MyRole[], Map<String, List<Role>>>{  
  10.   
  11.     @Override  
  12.     public Map<String, List<Role>> unmarshal(MyRole[] v) throws Exception {  
  13.           
  14.         Map<String, List<Role>> map=new HashMap<String, List<Role>>();  
  15.         for(int i=0;i<v.length;i++){  
  16.               
  17.             MyRole myRole = v[i];  
  18.             map.put(myRole.getKey(), myRole.getValue());  
  19.         }  
  20.         return map;  
  21.     }  
  22.   
  23.     @Override  
  24.     public MyRole[] marshal(Map<String, List<Role>> v) throws Exception {  
  25.           
  26.         MyRole[] myroles=new MyRole[v.size()];//定义一个实体类数组  
  27.         int i=0;  
  28.         for(String key:v.keySet()){  
  29.             myroles[i]=new MyRole();  
  30.             myroles[i].setKey(key);  
  31.             myroles[i].setValue(v.get(key));  
  32.             i++;  
  33.         }  
  34.         return myroles;  
  35.     }  
  36.   
  37.       
  38. }  

6:HelloWorld实现类

[java] view plain copy
  1. @Override  
  2.     public Map<String, List<Role>> getMap() {  
  3.           
  4.         Map<String, List<Role>> map=new HashMap<String, List<Role>>();  
  5.         List<Role> list=new ArrayList<Role>();  
  6.         list.add(new Role(1"总经理"));  
  7.         list.add(new Role(2"技术总监"));  
  8.         List<Role> list1=new ArrayList<Role>();  
  9.         list1.add(new Role(3"程序呀U呢"));  
  10.         map.put("qqw", list);  
  11.         map.put("qqw1", list1);  
  12.         return map;  
  13.     }  

7:接口的东西改了,需要重新在客户端自动生成一边代码,具体怎样自动参考上一篇文章,写一个main方法调用

[java] view plain copy
  1. HelloWorldService hService=new HelloWorldService();  
  2.         HelloWorld helloWorld = hService.getHelloWorldPort();  
  3.         User user=new User();  
  4.         user.setName("qqw1");  
  5.         List<Role> roleList = helloWorld.getUserRole(user);  
  6.         for(int i=0;i<roleList.size();i++){  
  7.             System.out.println("编号:"+roleList.get(i).getId()+"/角色名称:"+roleList.get(i).getName());  
  8.         }  
  9.         MyRoleArray map = helloWorld.getMap();  
  10.         List<MyRole> list = map.item;  
  11.         for(int i=0;i<list.size();i++){  
  12.             System.out.print(list.get(i).getKey()+":");  
  13.             for(Role r:list.get(i).getValue()){  
  14.                 System.out.print(r.getId()+","+r.getName());  
  15.             }  
  16.             System.out.println("================");  
  17.         }  

8:后台打印的东西


至此,CXF处理Map类型讲解完毕

原创粉丝点击