Java笔记-CXF使用Adapter处理复杂类型(如Map)
生活随笔
收集整理的這篇文章主要介紹了
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
即可
?
?
總結
以上是生活随笔為你收集整理的Java笔记-CXF使用Adapter处理复杂类型(如Map)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Arduino笔记-解决ESP8266上
- 下一篇: Fiddler工具杂记-将某些数据收集起