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

当有这个接口时:

Java笔记-CXF使用Adapter处理复杂类型(如Map)_第1张图片

Map>时报如下问题:

Java笔记-CXF使用Adapter处理复杂类型(如Map)_第2张图片

这里要使用Adapter进行处理:

接口换成:

@WebService
public interface MyService {

    public String say(String str);

    @XmlJavaTypeAdapter(MapAdapter.class)
    public Map> getRoles();
}

实现为:

@WebService
public class MyServiceImpl implements MyService {


    public String say(String str) {

        return "Hello" + str;
    }

    public Map> getRoles() {

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

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

        return map;
    }
}

其中MapAdapter.java

public class MapAdapter extends XmlAdapter>> {

    /***
     * 适配器转换 MyRole[] -> Map>
     * @param v
     * @return
     * @throws Exception
     */
    public Map> unmarshal(MyRole[] v) throws Exception {

        Map> map = new HashMap>();

        for(int i = 0; i < v.length; i++){

            MyRole r = v[i];
            map.put(r.getKey(), r.getValue());
        }

        return map;
    }


    /***
     * 适配器转换 Map> -> MyRole[]
     * @param v
     * @return
     * @throws Exception
     */
    public MyRole[] marshal(Map> 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 value;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public List getValue() {
        return value;
    }

    public void setValue(List 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

即可

Java笔记-CXF使用Adapter处理复杂类型(如Map)_第3张图片

 

 

你可能感兴趣的:(Web相关技术,webservice,Java,CXF,Java,webservice,web,http)