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;
    }

}

 

转载于:https://www.cnblogs.com/LIUWEI123/p/9622307.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值