zoukankan      html  css  js  c++  java
  • 学习webservice之cxf(5):cxf处理map等复杂类型


    Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions java.util.List是接口, 而 JAXB 无法处理接口。 this problem is related to the following location: at java.util.List at private java.util.Map com.rg2.webservice.jaxws_asm.GetrolesResponse._return at com.rg2.webservice.jaxws_asm.GetrolesResponse

    在cxf处理map类型的时候需要在接口上加注解,否则会报以上错误。

    @WebService
    public interface HelloWorld {
    
        public String say(String str);
        
        public List<Role> getRoleByUser(User user);
        
        /**
         * 获取所有用户对应的角色
         * @return
         */
        @XmlJavaTypeAdapter(MapAdapter.class)
        public Map<String, List<Role>> getroles();
    }

    其中MapAdapter单独写

    package com.rg2.adapter;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    import com.rg2.entity.MyRole;
    import com.rg2.entity.Role;
    
    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<String,List<Role>>();
            for (int i = 0; i < v.length; i++) {
                MyRole role = v[i];
                map.put(role.getKey(), role.getValue());
            }
            return map;
        }
    
        /**
         * 适配转换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;
        }
    
    }
    package com.rg2.entity;
    
    import java.util.List;
    
    /**
     * 自定义实体cxf能接受
     * @author Administrator
     *
     */
    public class MyRole {
    
        private String key;
        private List<Role> 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;
        }
    }

     启动server方法

    没有报错,打开浏览器

    如之前写的那样,将客户端使用wsdl2自动生成代码

    然后通过Client类调用服务端接口

    package com.rg2.webservice;
    
    import java.util.List;
    
    public class Client {
    
        public static void main(String[] args) {
            HelloWorldService service = new HelloWorldService();
            HelloWorld helloWorldPort = service.getHelloWorldPort();
    
            MyRoleArray getroles = helloWorldPort.getroles();
            List<MyRole> roleList = getroles.item;
            for (int i = 0; i < roleList.size(); i++) {
                MyRole my = roleList.get(i);
                System.out.print(my.key + ":");
                for (Role role : my.value) {
                    System.out.print(role.getId()+","+role.getRoleName());
                }
                System.out.println("===============");
            }
        }
    
    }

    运行结果为:

  • 相关阅读:
    xheditor编辑器自动上传外链图片及QQ截图等(升级支持webp格式)
    jQuery对checkbox的各种操作
    Jquery过滤选择器,选择前几个元素,后几个元素,内容过滤选择器等
    Java8新特性之forEach+Lambda 表达式遍历Map和List
    My97日期控件My97 DatePicker选择每月最后一天(周6周日不能选,节假日不能选,高亮每个月最后一个股票交易日)
    算法系列:日历算法
    mysql 将多个查询结果合并成一行
    js判断浏览器类型以及语言
    升级至 spring-5.3.0 关于 jdbcTemplate.query(sql, parameters, rowMapper) 的解决
    解决 redis Increased maximum number of open files to 10032 (it was originally set to 256).
  • 原文地址:https://www.cnblogs.com/zhengyuanyuan/p/9277059.html
Copyright © 2011-2022 走看看