CXF支持所有的自定义的对象以及常见的诸如List对象,可是对于Map CXF不支持,比如现在服务端根据客户端传递过来一个用户名返回一个MAP形式的权限集合,一发布就会报错,这是需要将map用适配器转换才能发布,本文将做介绍,项目框架沿用第一篇的。
服务端代码:
1:自定义一个MyRole对象
- package com.qqw.entity;
- import java.util.List;
- public class MyRole {
- private String key;
- private List<Role> value;
- public MyRole() {
- super();
- // TODO Auto-generated constructor stub
- }
- public MyRole(String key, List<Role> value) {
- super();
- this.key = key;
- this.value = value;
- }
- public String getKey() {
- return key;
- }
- public void setKey(String key) {
- this.key = key;
- }
- public List<Role> getValue() {
- return value;
- }
- public void setValue(List<Role> value) {
- this.value = value;
- }
- }
2:在接口HelloWorld返回map的方法里:
- package com.qqw.webservice;
- import java.util.List;
- import java.util.Map;
- import javax.jws.WebService;
- import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
- import com.qqw.entity.MapAdapter;
- import com.qqw.entity.Role;
- import com.qqw.entity.User;
- @WebService
- public interface HelloWorld {
- public String say(String str);
- public List<Role> getUserRole(User user);
- @XmlJavaTypeAdapter(MapAdapter.class) //采用适配器转换
- public Map<String, List<Role>> getMap();
- }
3:用户user
- package com.qqw.entity;
- public class User {
- private String name;
- private Integer id;
- public User() {
- super();
- // TODO Auto-generated constructor stub
- }
- public User(String name, Integer id) {
- super();
- this.name = name;
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- }
4:角色role
- package com.qqw.entity;
- public class Role {
- private Integer id;
- private String name;
- public Role() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Role(Integer id, String name) {
- super();
- this.id = id;
- this.name = name;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
5:定义适配器
- package com.qqw.entity;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.xml.bind.annotation.adapters.XmlAdapter;
- public class MapAdapter extends XmlAdapter<MyRole[], Map<String, List<Role>>>{
- @Override
- public Map<String, List<Role>> unmarshal(MyRole[] v) throws Exception {
- Map<String, List<Role>> map=new HashMap<String, List<Role>>();
- for(int i=0;i<v.length;i++){
- MyRole myRole = v[i];
- map.put(myRole.getKey(), myRole.getValue());
- }
- return map;
- }
- @Override
- public MyRole[] marshal(Map<String, List<Role>> v) throws Exception {
- MyRole[] myroles=new MyRole[v.size()];//定义一个实体类数组
- int i=0;
- for(String key:v.keySet()){
- myroles[i]=new MyRole();
- myroles[i].setKey(key);
- myroles[i].setValue(v.get(key));
- i++;
- }
- return myroles;
- }
- }
6:HelloWorld实现类
- @Override
- public Map<String, List<Role>> getMap() {
- Map<String, List<Role>> map=new HashMap<String, List<Role>>();
- List<Role> list=new ArrayList<Role>();
- list.add(new Role(1, "总经理"));
- list.add(new Role(2, "技术总监"));
- List<Role> list1=new ArrayList<Role>();
- list1.add(new Role(3, "程序呀U呢"));
- map.put("qqw", list);
- map.put("qqw1", list1);
- return map;
- }
7:接口的东西改了,需要重新在客户端自动生成一边代码,具体怎样自动参考上一篇文章,写一个main方法调用
- HelloWorldService hService=new HelloWorldService();
- HelloWorld helloWorld = hService.getHelloWorldPort();
- User user=new User();
- user.setName("qqw1");
- List<Role> roleList = helloWorld.getUserRole(user);
- for(int i=0;i<roleList.size();i++){
- System.out.println("编号:"+roleList.get(i).getId()+"/角色名称:"+roleList.get(i).getName());
- }
- MyRoleArray map = helloWorld.getMap();
- List<MyRole> list = map.item;
- for(int i=0;i<list.size();i++){
- System.out.print(list.get(i).getKey()+":");
- for(Role r:list.get(i).getValue()){
- System.out.print(r.getId()+","+r.getName());
- }
- System.out.println("================");
- }
8:后台打印的东西
至此,CXF处理Map类型讲解完毕