CXF|CXF WebService中传递复杂对象(List、Map、Array)

转自:https://wenku.baidu.com/view/047ce58ed0d233d4b14e69eb.html
现在开始介绍传递复杂类型的对象。如JavaBean、Array、List、Map等。
如有不正确之处,请各位给予指出!
1、 首先看看服务器端的代码所需要的JavaBean对象

1 1.package com.wy.object.entry; 2 2. 3 3.import java.io.Serializable; 4 4. 5 5./** 6 6.* User Entity 7 7.* @author Administrator 8 8.* 9 9.*/ 10 10.public class User implements Serializable { 11 11. 12 12.private static final long serialVersionUID = 1L; 13 13. 14 14.private int id; 15 15.private String name; 16 16.private String email; 17 17.private String address; 18 18. 19 19.public int getId() { 20 20.return id; 21 21.} 22 22. 23 23.public void setId(int id) { 24 24.this.id = id; 25 25.} 26 26. 27 27.public String getName() { 28 28.return name; 29 29.} 30 30. 31 31.public void setName(String name) { 32 32.this.name = name; 33 33.} 34 34. 35 35.public String getEmail() { 36 36.return email; 37 37.} 38 38. 39 39.public void setEmail(String email) { 40 40.this.email = email; 41 41.} 42 42. 43 43.public String getAddress() { 44 44.return address; 45 45.} 46 46. 47 47.public void setAddress(String address) { 48 48.this.address = address; 49 49.} 50 50. 51 51.@Override 52 52.public String toString() { 53 53. 54 54.return this.id + " - " + this.name + " - " + this.address + " - " 55 55.+ this.email; 56 56. 57 57.} 58 58.}

下面的是集合传递Users,CXF直接传递集合对象会出现异常,用一个对象包装下就Ok了,不知道是什么原因。
1 1.package com.wy.object; 2 2. 3 3.import java.util.HashMap; 4 4.import java.util.List; 5 5. 6 6.import com.wy.object.entry.User; 7 7. 8 8./** 9 9.* CXF直接传递集合对象会出现异常,用一个对象包装下就Ok了,不知道是什么原因? 10 10.* 11 11.* @author Administrator 12 12.* 13 13.*/ 14 14.public class Users { 15 15.private List users; 16 16.private User[] userArr; 17 17.private HashMap maps; 18 18.public List getUsers() { 19 19.return users; 20 20.} 21 21. 22 22.public void setUsers(List users) { 23 23.this.users = users; 24 24.} 25 25. 26 26.public User[] getUserArr() { 27 27.return userArr; 28 28.} 29 29. 30 30.public void setUserArr(User[] userArr) { 31 31.this.userArr = userArr; 32 32.} 33 33. 34 34.public HashMap getMaps() { 35 35.return maps; 36 36.} 37 37. 38 38.public void setMaps(HashMap maps) { 39 39.this.maps = maps; 40 40.} 41 41. 42 42.}

2、 下面看看复杂对象传递的服务器端代码
1 1.package com.wy.object.server; 2 2. 3 3.import javax.jws.WebParam; 4 4.import javax.jws.WebService; 5 5. 6 6.import com.wy.object.Users; 7 7.import com.wy.object.entry.User; 8 8./** 9 9.* 定制客户端请求WebService所需要的接口 10 10.* @author Administrator 11 11.* 12 12.*/ 13 13. 14 14.@WebService 15 15.public interface IUserService { 16 16.public User getUserByName(@WebParam(name = "name") String name); 17 17.public void setUser(User user); 18 18.public Users getUsers(int i); 19 19.public void setUsers(Users users); 20 20.public Users getUserArray(int i); 21 21.public void setUserArray(Users users); 22 22.public void setUserMap(Users maps); 23 23.public Users getUserMap(); 24 24.}

【CXF|CXF WebService中传递复杂对象(List、Map、Array)】
接口实现类:
1 1.package com.wy.object.server.impl; 2 2. 3 3.import java.util.ArrayList; 4 4.import java.util.Date; 5 5.import java.util.HashMap; 6 6.import java.util.List; 7 7. 8 8.import javax.jws.WebParam; 9 9.import javax.jws.WebService; 10 10.import javax.jws.soap.SOAPBinding; 11 11.import javax.jws.soap.SOAPBinding.Style; 12 12. 13 13.import com.wy.object.Users; 14 14.import com.wy.object.entry.User; 15 15.import com.wy.object.server.IUserService; 16 16. 17 17./** 18 18.* WebService传递复杂对象,如JavaBean、Array、List、Map等 19 19.* 20 20.* @author Administrator 21 21.* 22 22.*/ 23 23. 24 24.@WebService(endpointInterface = "com.wy.object.server.IUserService", serviceName = "IUserService") 25 25.@SOAPBinding(style = Style.RPC) 26 26.@SuppressWarnings("deprecation") 27 27.public class UserService implements IUserService { 28 28. 29 29.// string 30 30.public User getUserByName(@WebParam(name = "name") 31 31.String name) { 32 32.User user = new User(); 33 33.user.setId(new Date().getSeconds()); 34 34.user.setName(name); 35 35.user.setAddress("China BeiJing"); 36 36.user.setEmail(name + "@163.com"); 37 37.return user; 38 38.} 39 39. 40 40.// object JavaBean 41 41.public void setUser(User user) { 42 42.System.out.println("@@@@@@@@@@@@ Server setUser @@@@@@@@@@@"); 43 43.System.out.println("setUser:" + user); 44 44.} 45 45. 46 46.// 传递集合的包装对象 List 47 47.public Users getUsers(int i) { 48 48.List users = new ArrayList(); 49 49.for (int j = 0; j <= i; j++) { 50 50.User user = new User(); 51 51.user.setId(new Date().getSeconds()); 52 52.user.setName("Sun#" + j); 53 53.user.setAddress("China"); 54 54.user.setEmail("Sun" + j + "@163.com"); 55 55. 56 56.users.add(user); 57 57. 58 58.} 59 59.Users u = new Users(); 60 60.u.setUsers(users); 61 61.return u; 62 62.} 63 63. 64 64.public void setUsers(Users users) { 65 65.System.out.println("############ Server setUsers ###########"); 66 66.for (User u : users.getUsers()) { 67 67.System.out.println("setUsers:" + u); 68 68.} 69 69.} 70 70. 71 71.// 传递集合的包装对象 Array 72 72.public Users getUserArray(int i) { 73 73.User[] users = new User[i]; 74 74.for (int j = 0; j < i; j++) { 75 75.User user = new User(); 76 76.user.setId(new Date().getSeconds()); 77 77.user.setName("Jack#" + j); 78 78.user.setAddress("China"); 79 79.user.setEmail("Jack" + j + "@163.com"); 80 80. 81 81.users[j] = user; 82 82.} 83 83.Users u = new Users(); 84 84.u.setUserArr(users); 85 85.return u; 86 86.} 87 87. 88 88.public void setUserArray(Users users) { 89 89.System.out.println("$$$$$$$$$$$$ Server setUserArray $$$$$$$$"); 90 90.for (User u : users.getUserArr()) { 91 91.System.out.println("setUserArray:" + u); 92 92.} 93 93.} 94 94. 95 95.// 传递集合的包装对象 Map 96 96.public void setUserMap(Users maps) { 97 97.System.out.println("------------ Server setUserMap ----------"); 98 98.System.out.println("setUserMap:" + maps.getMaps()); 99 99.} 100 100. 101 101.public Users getUserMap() { 102 102.HashMap users = new HashMap(); 103 103.User user = new User(); 104 104.user.setId(new Date().getSeconds()); 105 105.user.setName("Jack#"); 106 106.user.setAddress("China#"); 107 107.user.setEmail("Jack@163.com"); 108 108. 109 109.users.put("A", user); 110 110.user = new User(); 111 111.user.setId(new Date().getSeconds()); 112 112.user.setName("Tom"); 113 113.user.setAddress("China$$"); 114 114.user.setEmail("Tom@163.com"); 115 115. 116 116.users.put("B", user); 117 117.Users u = new Users(); 118 118.u.setMaps(users); 119 119. 120 120.return u; 121 121.} 122 122.}

3、 发布WebService的代码
1 1.package com.wy.object; 2 2. 3 3.import javax.xml.ws.Endpoint; 4 4. 5 5.import com.wy.object.server.impl.UserService; 6 6./** 7 7.* 发布UserService服务 8 8.* @author Administrator 9 9.* 10 10.*/ 11 11.public class DeployUserService { 12 12.public static void main(String[] args) throws InterruptedException { 13 13.String address = "http://localhost:8080/WebServiceCXF/services/IUserService"; 14 14.UserService service = new UserService(); 15 15.Endpoint.publish(address, service); 16 16.System.out.println("Server ready..."); 17 17.Thread.sleep(1000 * 60 * 20); 18 18.System.out.println("Server exit..."); 19 19.System.exit(0); 20 20.} 21 21.}

4、 客户端调用代码
1 1.package com.wy.object.client; 2 2. 3 3.import java.util.ArrayList; 4 4.import java.util.List; 5 5.import java.util.Random; 6 6. 7 7.import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; 8 8. 9 9.import com.wy.object.Users; 10 10.import com.wy.object.entry.User; 11 11.import com.wy.object.server.IUserService; 12 12. 13 13./** 14 14.* 调用UserService客户端 15 15.* 16 16.* @author Administrator 17 17.* 18 18.*/ 19 19.public class UserServiceClient { 20 20.public static void main(String[] args) { 21 21. 22 22.// 调用WebService 23 23.JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); 24 24.factory.setServiceClass(IUserService.class); 25 25.factory.setAddress("http://localhost:8080/WebServiceCXF/services/IUserService"); 26 26.IUserService service = (IUserService) factory.create(); 27 27. 28 28.//string 29 29.System.out.println("@@@@@@@@@@@@@Client getUserByName@@@@@@@@@@@@@@"); 30 30.User user = service.getUserByName("Jack"); 31 31.System.out.println("Client: "+ user); 32 32.user.setAddress("China-BeiJing"); 33 33. 34 34.//object JavaBean 35 35.service.setUser(user); 36 36. 37 37.//传递集合的包装对象 List 38 38.System.out.println("#############Client getUsers###################"); 39 39.Users users = service.getUsers(4); 40 40.System.out.println("Client: "+ users); 41 41.List tempUsers = new ArrayList(); 42 42.for (User u : users.getUsers()) { 43 43.System.out.println("Client: "+ u); 44 44.u.setName("Jack" + new Random().nextInt(100)); 45 45.u.setAddress("Chian-BeiJing#" + new Random().nextInt(100)); 46 46.tempUsers.add(u); 47 47.} 48 48.users.getUsers().clear(); 49 49.users.getUsers().addAll(tempUsers); 50 50. 51 51.service.setUsers(users); 52 52. 53 53.//传递集合的包装对象 Array 54 54.System.out.println("$$$$$$$$$$$$Client getUserArray$$$$$$$$$$$$$$$"); 55 55.users = service.getUserArray(4); 56 56.User[] userArr = new User[4]; 57 57.int i = 0; 58 58.for (User u : users.getUserArr()) { 59 59.System.out.println("Client: "+ u); 60 60.u.setName("Jack" + new Random().nextInt(100)); 61 61.u.setAddress("Chian-ShenZhen#" + new Random().nextInt(100)); 62 62.userArr[i] = u; 63 63.i++; 64 64.} 65 65.users.setUserArr(userArr); 66 66. 67 67.service.setUserArray(users); 68 68. 69 69.//传递集合的包装对象 Map 70 70.System.out.println("------------Client getUserMap----------------"); 71 71.users = service.getUserMap(); 72 72.System.out.println("Client: "+ users.getMaps()); 73 73.users.getMaps().put("ABA", userArr[0]); 74 74.service.setUserMap(users); 75 75. 76 76.} 77 77.}


    推荐阅读