zoukankan      html  css  js  c++  java
  • WebService小白学习 之 处理一些Map等复杂类型 (5)

    上篇 WebService小白学习 之 处理JavaBean以及复合类型,list

    该篇为CXF处理一些Map等复杂类型的方法。

    实现过程:

    1、在服务端项目IHelloWorld.java添加方法声明getRoles();

    package com.gx.webservice;
    
    import java.util.List;
    import java.util.Map;
    
    import javax.jws.WebService;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    import com.gx.adapter.MapAdapter;
    import com.gx.entity.Role;
    import com.gx.entity.User;
    
    @WebService
    public interface IHelloWorld {
    	
    	public String say(String str);
    	
    	public List<Role> getRoleByUser(User user);//通过用户获取角色
    	
    	@XmlJavaTypeAdapter(MapAdapter.class)
    	public Map<String, List<Role>> getRoles(); //获取所有用户以及对应的角色
    
    }
    

    2、在服务端项目创建com.gx.adapter包,创建MapAdapter.java

    package com.gx.adapter;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    import com.gx.entity.MyRole;
    import com.gx.entity.Role;
    
    /**
     * 适配器
    * @ClassName: MapAdapter 
    * @Description: 适配转换
    * @author zhoujie 
    * @date 2018年7月27日 下午7:58:46 
    * @version V1.0
     */
    public class MapAdapter extends XmlAdapter<MyRole[], Map<String, List<Role>>>{
    
    	/**
    	 * 适配转换 MyRole[] -> Map<String,List<Role>>
    	 */
    	@Override
    	public Map<String, List<Role>> unmarshal(MyRole[] v) throws Exception {
    		Map<String, List<Role>> map = new HashMap<>();
    		for (int i = 0; i < v.length; i++) {
    			MyRole r = v[i];
    			map.put(r.getKey(), r.getValue());
    		}
    		return null;
    	}
    
    	/**
    	 * 适配转换Map<String,List<Role>> ->  MyRole[]
    	 */
    	@Override
    	public MyRole[] marshal(Map<String, List<Role>> v) throws Exception {
    		MyRole[] roles = new MyRole[v.size()];
    		int i = 0;
    		for (String key : v.keySet()) {
    			roles[i] = new MyRole();
    			roles[i].setKey(key);
    			roles[i].setValue(v.get(key));
    			i++;
    		}
    		return roles;
    	}
    
    }
    

    3、在服务端项目创建com.gx.entity包,创建MyRole.java

    package com.gx.entity;
    
    import java.util.List;
    
    /**
     * 
    * @ClassName: MyRole 
    * @Description: map适配器--自定义实体
    * @author zhoujie 
    * @date 2018年7月27日 下午7:50:17 
    * @version V1.0
     */
    public class MyRole {
    	
    	private String key; //相当于map的key
    	private List<Role> value; //相当于map的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;
    	}
    	
    }
    

    4、在服务端项目HelloWorldImpl.java添加方法实现getRoles();

    package com.gx.webservice.impl;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.jws.WebService;
    
    import com.gx.entity.Role;
    import com.gx.entity.User;
    import com.gx.webservice.IHelloWorld;
    
    @WebService
    public class HelloWorldImpl implements IHelloWorld{
    
    	public String say(String str) {
    		return "hello "+str;
    	}
    
    	@Override
    	public List<Role> getRoleByUser(User user) {
    		//模拟 数据
    		List<Role> rolelist = new ArrayList<>();
    		if(user!=null){
    			if(user.getUsername().equals("sa") && user.getPassword().equals("123")){
    				rolelist.add(new Role(1, "技术总监"));
    				rolelist.add(new Role(2, "架构师"));
    			}else if(user.getUsername().equals("zj") && user.getPassword().equals("123")){
    				rolelist.add(new Role(3, "程序员"));
    			}
    		}
    		return rolelist;
    	}
    
    	@Override
    	public Map<String, List<Role>> getRoles() {
    		//模拟 数据
    		Map<String, List<Role>> map = new HashMap<>();
    		List<Role> rolelist = new ArrayList<>();
    		rolelist.add(new Role(1, "技术总监"));
    		rolelist.add(new Role(2, "架构师"));
    		map.put("sa", rolelist);
    		List<Role> rolelist2 = new ArrayList<>();
    		rolelist2.add(new Role(3, "程序员"));
    		map.put("zj", rolelist2);
    		return map;
    	}
    
    }
    

    5、客户端项目重新使用cxf工具生成文件,忘记请看这里

    6、客户端测试Client.java

    package com.gx.webservice;
    
    import java.util.List;
    
    public class Client {
    	
    	public static void main(String[] args) {
    		
    		IHelloWorldService service = new IHelloWorldService();
    		IHelloWorld helloworld = service.getIHelloWorldPort();
    		//System.out.println(helloworld.say("zj"));
    		
    		/*User user = new User();
    		user.setUsername("sa");
    		user.setPassword("123");
    		List<Role> rolelist = helloworld.getRoleByUser(user);
    		for (Role role : rolelist) {
    			System.out.println(role.getId()+","+role.getRoleName());
    		}*/
    		
    		MyRoleArray array = helloworld.getRoles();
    		List<MyRole> roleList = array.item;
    		for (MyRole myRole : roleList) {
    			System.out.print(myRole.key+":");
    			for (Role role : myRole.value) {
    				System.out.print(role.getId()+","+role.getRoleName());
    			}
    			System.out.println("=================");
    		}
     		
    	}
    	
    }
    

    结果:

    下篇

    WebService小白学习 之 CXF添加拦截器,自定义拦截器

  • 相关阅读:
    BZOJ 1101 莫比乌斯函数+分块
    BZOJ 2045 容斥原理
    BZOJ 4636 (动态开节点)线段树
    BZOJ 2005 容斥原理
    BZOJ 2190 欧拉函数
    BZOJ 2818 欧拉函数
    BZOJ 3123 主席树 启发式合并
    812. Largest Triangle Area
    805. Split Array With Same Average
    794. Valid Tic-Tac-Toe State
  • 原文地址:https://www.cnblogs.com/qq1995/p/10358986.html
Copyright © 2011-2022 走看看