zoukankan      html  css  js  c++  java
  • WebServce之Map类型传输

    cxf在传输java类型时基本类型都可以传输,但有些类型的不可以传输,比如map类型

    接口:

    import java.util.List;
    import java.util.Map;
    
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    
    @WebService
    public interface Service {
        @WebMethod
        public String test(String word) ;
        @WebMethod
        public List<Student> getStudents();
        
        public Map<String,List<Student>> getStudentsGroupByClass();
    }

    实现类

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.jws.WebService;
    
    @WebService
    public class ServiceImpl implements Service {
    
        public List<Student> getStudents() {
            List<Student> students = new ArrayList<Student>();
            students.add(new Student("aa", "aa"));
            students.add(new Student("bb", "bb"));
            students.add(new Student("cc", "cc"));
            students.add(new Student("dd", "dd"));
    
            return students;
        }
    
        public String test(String word) {
            return "hello" + word;
        }
    
        public Map<String, List<Student>> getStudentsGroupByClass() {
            Map<String, List<Student>> students = new HashMap<String, List<Student>>();
            students.put("一班", getStudents());
            List<Student> stus = new ArrayList<Student>();
            stus.add(new Student("AA", "AA"));
            stus.add(new Student("BB", "BB"));
            stus.add(new Student("CC", "CC"));
            stus.add(new Student("DD", "DD"));
            students.put("二班", stus);
            return students;
        }
    
    }

    当启动发布时报错:

      可以通过自定义转化器解决发布问题

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class CustomerMapAdapter extends XmlAdapter<StudentArray[], Map<String, List<Student>>> {
    
        @Override
        public StudentArray[] marshal(Map<String, List<Student>> studentmap) throws Exception {
            StudentArray[] students = new StudentArray[studentmap.size()];
            Set<String> keys = studentmap.keySet();
            int i = 0;
            for (String key : keys) {
                students[i].setClassname(key);
                students[i].setStudents(studentmap.get(key));
                i++;
            }
            return students;
        }
    
        @Override
        public Map<String, List<Student>> unmarshal(StudentArray[] students) throws Exception {
            Map<String, List<Student>> map = new HashMap<String, List<Student>>();
            for (StudentArray stu : students) {
                map.put(stu.getClassname(), stu.getStudents());
            }
            return map;
        }
    
    }
  • 相关阅读:
    linux下小知识点积累
    马斯洛需求层次理论
    tar命令的小经验
    shell 和c语言的区别
    使用vue实现的品牌列表简单小例子
    vue的基本代码以及常见指令
    MVC和MVVM
    CSS3幽灵
    Web版App,原生App,混合App的区别以及优缺点
    常见的sql操作
  • 原文地址:https://www.cnblogs.com/LIUWEI123/p/9622307.html
Copyright © 2011-2022 走看看