Java笔记-CXF使用Adapter处理复杂类型(如Map)

当有这个接口时:

Map<String, List<Role>>时报如下问题:

这里要使用Adapter进行处理:

接口换成:

@WebService
public interface MyService {

    public String say(String str);

    @XmlJavaTypeAdapter(MapAdapter.class)
    public Map<String, List<Role>> getRoles();
}

实现为:

@WebService
public class MyServiceImpl implements MyService {


    public String say(String str) {

        return "Hello" + str;
    }

    public Map<String, List<Role>> getRoles() {

        Map<String, List<Role>> map = new HashMap<String, List<Role>>();
        List<Role> roleList1 = new ArrayList<Role>();
        roleList1.add(new Role(1, "架构师"));
        roleList1.add(new Role(2, "技术总监"));
        map.put("xxx", roleList1);

        List<Role> roleList2 = new ArrayList<Role>();
        roleList2.add(new Role(3, "程序员"));
        map.put("yyy", roleList2);

        return map;
    }
}

其中MapAdapter.java

public class MapAdapter extends XmlAdapter<MyRole[], Map<String, List<Role>>> {

    /***
     * 适配器转换 MyRole[] -> Map<String, List<Role>>
     * @param v
     * @return
     * @throws Exception
     */
    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 r = v[i];
            map.put(r.getKey(), r.getValue());
        }

        return map;
    }


    /***
     * 适配器转换 Map<String, List<Role>> -> MyRole[]
     * @param v
     * @return
     * @throws Exception
     */
    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));
        }

        return roles;
    }
}

MyRole.java

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

Role.java

public class Role {

    private Integer id;
    private String roleName;

    public Role(){

    }

    public Role(Integer id, String roleName) {
        this.id = id;
        this.roleName = roleName;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
}

User.java

public class User {

    private Integer id;
    private String userName;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

源码打包下载地址为:

https://github.com/fengfanchen/Java/tree/master/CXFServiceAdapter

即可

 

 

©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值