zoukankan      html  css  js  c++  java
  • (四)CXF之处理Map类型的数据

    一、需求描述

    • 正常来讲webService可以处理Java 数据类型、JavaBean、List等,但是却不能处理Map数据类型。本章讲解如何使用适配器来使得web服务可以处理Map数据类型。
    • 流程:
    1. 在web服务接口类中添加@XmlJavaTypeAdapter注解

    @XmlJavaTypeAdapter(MapAdapter.class)
        @WebResult(name="getRolesResult")
        public Map<String,List<Role>> getRoles();

      2. 编写适配器类

       

    二、案例

      2.1  服务端

    • Role.java
    public class Role {
        private Integer id;
        private String roleName;
    
        public Role() {
            super();
        }
    
        public Role(Integer id, String roleName) {
            super();
            this.id = id;
            this.roleName = roleName;
        }
    
       //省略getset方法 
    • User.java
    public class User {
        private Integer id;
        private String userName;
        private String passWord;
    
    //隐藏getset方法
    • 编写服务接口
    package com.shyroke.service;
    
    import java.util.List;
    import java.util.Map;
    
    import javax.jws.WebResult;
    import javax.jws.WebService;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    import com.shyroke.bean.Role;
    
    @WebService
    public interface ILogin {
    
        @XmlJavaTypeAdapter(MapAdapter.class)
        @WebResult(name="getRolesResult")
        public Map<String,List<Role>> getRoles();
    }
    • 编写服务接口实现类

    package com.shyroke.service;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.jws.WebService;
    
    import com.shyroke.bean.Role;
    import com.shyroke.bean.User;
    
    @WebService(endpointInterface = "com.shyroke.service.ILogin")
    public class Login implements ILogin {
    
        /**
         * 获取所有用户以及对应的角色
         */
        public Map<String, List<Role>> getRoles() {
            Map<String, List<Role>> map = new HashMap<String, List<Role>>();
    
            List<Role> roleList1 = new ArrayList<Role>();
            roleList1.add(new Role(1, "架构师"));
            roleList1.add(new Role(2, "技术总监"));
    
            List<Role> roleList2 = new ArrayList<Role>();
            roleList2.add(new Role(3, "程序员"));
    
            map.put("张三", roleList1);
            map.put("李四", roleList2);
    
            return map;
        }
    
    }
    • 编写适配器

    package com.shyroke.service;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    import com.shyroke.bean.MyRole;
    import com.shyroke.bean.Role;
    
    public class MapAdapter extends XmlAdapter<MyRole[], Map<String, List<Role>>> {
    
        /**
         * 适配转换 MyRole[] -> Map<String, List<Role>>
         */
        @Override
        public MyRole[] marshal(Map<String, List<Role>> roleMap) throws Exception {
            MyRole[] roles = new MyRole[roleMap.size()];
    
            int i = 0;
            for (String key : roleMap.keySet()) {
                roles[i] = new MyRole();
                roles[i].setKey(key);
                roles[i].setValue(roleMap.get(key));
                i++;
            }
    
            return roles;
        }
    
        /**
         * 适配转换 Map<String, List<Role>> -> MyRole[]
         */
        @Override
        public Map<String, List<Role>> unmarshal(MyRole[] roles) throws Exception {
    
            Map<String, List<Role>> map = new HashMap<String, List<Role>>();
    
            for (int i = 0; i < roles.length; i++) {
                map.put(roles[i].getKey(), roles[i].getValue());
            }
    
            return map;
        }
    
    }
    • MyRole.java
    /**
     * 本类用于webService的Map类型的数据转换。
     * @author Administrator
     *
     */
    public class MyRole {
        private String key;
        private List<Role> value;
    
    //隐藏getset方法
    }
    • 发布服务

    package com.shyroke.publish;
    
    
    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
    
    import com.shyroke.service.ILogin;
    import com.shyroke.service.Login;
    
    public class PublishMain {
        public static void main(String[] args) {
            
            String address="http://localhost:3333/login";
            JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
            factoryBean.setAddress(address);
            factoryBean.setServiceClass(ILogin.class);
            factoryBean.setServiceBean(new Login());
            
            factoryBean.create();
            System.out.println("服务发布.......");
            
        }
    }

        2.2  客户端

    •  用cxf工具生成client文件

     

    •  测试

    package com.shyroke.service;
    
    import java.util.List;
    
    public class Client {
        public static void main(String[] args) {
            ILogin login=new ILoginService().getILoginPort();
            MyRoleArray roles=login.getRoles();
            List<MyRole> roleList= roles.item;
            for(MyRole role:roleList) {
                System.out.println(role.getKey());
                for(Role r:role.getValue()) {
                    System.out.println(r.getId()+"	"+r.getRoleName());
                }
                System.out.println("------------------");
            }
        }
    }

  • 相关阅读:
    HDU1263(map)
    Let the Balloon Rise HDU
    PAT甲级1007
    PAT乙级1041
    PAT乙级1037
    PAT乙级1031
    PAT乙级1025
    Hanoi(栈实现)
    《全球资产配置》读后感 读书笔记
    《与内心的恐惧对话》读后感 读书笔记
  • 原文地址:https://www.cnblogs.com/shyroke/p/7966045.html
Copyright © 2011-2022 走看看