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