Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptionsjava.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类型的时候需要在接口上加注解,否则会报以上错误。
@WebServicepublic 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("==============="); ???????} ???}}
运行结果为:
学习webservice之cxf(5):cxf处理map等复杂类型
原文地址:https://www.cnblogs.com/zhengyuanyuan/p/9277059.html