转自:https://wenku.baidu.com/view/047ce58ed0d233d4b14e69eb.html
现在开始介绍传递复杂类型的对象。如JavaBean、Array、List、Map等。
如有不正确之处,请各位给予指出!
1、 首先看看服务器端的代码所需要的JavaBean对象
1. package com.wy.object.entry;
2.
3. import java.io.Serializable;
4.
5. /**
6. * User Entity
7. * @author Administrator
8. *
9. */
10. public class User implements Serializable {
11.
12. private static final long serialVersionUID = 1L;
13.
14. private int id;
15. private String name;
16. private String email;
17. private String address;
18.
19. public int getId() {
20. return id;
21. }
22.
23. public void setId(int id) {
24. this.id = id;
25. }
26.
27. public String getName() {
28. return name;
29. }
30.
31. public void setName(String name) {
32. this.name = name;
33. }
34.
35. public String getEmail() {
36. return email;
37. }
38.
39. public void setEmail(String email) {
40. this.email = email;
41. }
42.
43. public String getAddress() {
44. return address;
45. }
46.
47. public void setAddress(String address) {
48. this.address = address;
49. }
50.
51. @Override
52. public String toString() {
53.
54. return this.id + " - " + this.name + " - " + this.address + " - "
55. + this.email;
56.
57. }
58. }
下面的是集合传递Users,CXF直接传递集合对象会出现异常,用一个对象包装下就Ok了,不知道是什么原因。
1. package com.wy.object;
2.
3. import java.util.HashMap;
4. import java.util.List;
5.
6. import com.wy.object.entry.User;
7.
8. /**
9. * CXF直接传递集合对象会出现异常,用一个对象包装下就Ok了,不知道是什么原因?
10. *
11. * @author Administrator
12. *
13. */
14. public class Users {
15. private List<User> users;
16. private User[] userArr;
17. private HashMap<String, User> maps;
18. public List<User> getUsers() {
19. return users;
20. }
21.
22. public void setUsers(List<User> users) {
23. this.users = users;
24. }
25.
26. public User[] getUserArr() {
27. return userArr;
28. }
29.
30. public void setUserArr(User[] userArr) {
31. this.userArr = userArr;
32. }
33.
34. public HashMap<String, User> getMaps() {
35. return maps;
36. }
37.
38. public void setMaps(HashMap<String, User> maps) {
39. this.maps = maps;
40. }
41.
42. }
2、 下面看看复杂对象传递的服务器端代码
1. package com.wy.object.server;
2.
3. import javax.jws.WebParam;
4. import javax.jws.WebService;
5.
6. import com.wy.object.Users;
7. import com.wy.object.entry.User;
8. /**
9. * 定制客户端请求WebService所需要的接口
10. * @author Administrator
11. *
12. */
13.
14. @WebService
15. public interface IUserService {
16. public User getUserByName(@WebParam(name = "name") String name);
17. public void setUser(User user);
18. public Users getUsers(int i);
19. public void setUsers(Users users);
20. public Users getUserArray(int i);
21. public void setUserArray(Users users);
22. public void setUserMap(Users maps);
23. public Users getUserMap();
24. }
接口实现类:
1. package com.wy.object.server.impl;
2.
3. import java.util.ArrayList;
4. import java.util.Date;
5. import java.util.HashMap;
6. import java.util.List;
7.
8. import javax.jws.WebParam;
9. import javax.jws.WebService;
10. import javax.jws.soap.SOAPBinding;
11. import javax.jws.soap.SOAPBinding.Style;
12.
13. import com.wy.object.Users;
14. import com.wy.object.entry.User;
15. import com.wy.object.server.IUserService;
16.
17. /**
18. * WebService传递复杂对象,如JavaBean、Array、List、Map等
19. *
20. * @author Administrator
21. *
22. */
23.
24. @WebService(endpointInterface = "com.wy.object.server.IUserService", serviceName = "IUserService")
25. @SOAPBinding(style = Style.RPC)
26. @SuppressWarnings("deprecation")
27. public class UserService implements IUserService {
28.
29. // string
30. public User getUserByName(@WebParam(name = "name")
31. String name) {
32. User user = new User();
33. user.setId(new Date().getSeconds());
34. user.setName(name);
35. user.setAddress("China BeiJing");
36. user.setEmail(name + "@163.com");
37. return user;
38. }
39.
40. // object JavaBean
41. public void setUser(User user) {
42. System.out.println("@@@@@@@@@@@@ Server setUser @@@@@@@@@@@");
43. System.out.println("setUser:" + user);
44. }
45.
46. // 传递集合的包装对象 List
47. public Users getUsers(int i) {
48. List<User> users = new ArrayList<User>();
49. for (int j = 0; j <= i; j++) {
50. User user = new User();
51. user.setId(new Date().getSeconds());
52. user.setName("Sun#" + j);
53. user.setAddress("China");
54. user.setEmail("Sun" + j + "@163.com");
55.
56. users.add(user);
57.
58. }
59. Users u = new Users();
60. u.setUsers(users);
61. return u;
62. }
63.
64. public void setUsers(Users users) {
65. System.out.println("############ Server setUsers ###########");
66. for (User u : users.getUsers()) {
67. System.out.println("setUsers:" + u);
68. }
69. }
70.
71. // 传递集合的包装对象 Array
72. public Users getUserArray(int i) {
73. User[] users = new User[i];
74. for (int j = 0; j < i; j++) {
75. User user = new User();
76. user.setId(new Date().getSeconds());
77. user.setName("Jack#" + j);
78. user.setAddress("China");
79. user.setEmail("Jack" + j + "@163.com");
80.
81. users[j] = user;
82. }
83. Users u = new Users();
84. u.setUserArr(users);
85. return u;
86. }
87.
88. public void setUserArray(Users users) {
89. System.out.println("$$$$$$$$$$$$ Server setUserArray $$$$$$$$");
90. for (User u : users.getUserArr()) {
91. System.out.println("setUserArray:" + u);
92. }
93. }
94.
95. // 传递集合的包装对象 Map
96. public void setUserMap(Users maps) {
97. System.out.println("------------ Server setUserMap ----------");
98. System.out.println("setUserMap:" + maps.getMaps());
99. }
100.
101. public Users getUserMap() {
102. HashMap<String, User> users = new HashMap<String, User>();
103. User user = new User();
104. user.setId(new Date().getSeconds());
105. user.setName("Jack#");
106. user.setAddress("China#");
107. user.setEmail("Jack@163.com");
108.
109. users.put("A", user);
110. user = new User();
111. user.setId(new Date().getSeconds());
112. user.setName("Tom");
113. user.setAddress("China$$");
114. user.setEmail("Tom@163.com");
115.
116. users.put("B", user);
117. Users u = new Users();
118. u.setMaps(users);
119.
120. return u;
121. }
122. }
3、 发布WebService的代码
1. package com.wy.object;
2.
3. import javax.xml.ws.Endpoint;
4.
5. import com.wy.object.server.impl.UserService;
6. /**
7. * 发布UserService服务
8. * @author Administrator
9. *
10. */
11. public class DeployUserService {
12. public static void main(String[] args) throws InterruptedException {
13. String address = "http://localhost:8080/WebServiceCXF/services/IUserService";
14. UserService service = new UserService();
15. Endpoint.publish(address, service);
16. System.out.println("Server ready...");
17. Thread.sleep(1000 * 60 * 20);
18. System.out.println("Server exit...");
19. System.exit(0);
20. }
21. }
4、 客户端调用代码
1. package com.wy.object.client;
2.
3. import java.util.ArrayList;
4. import java.util.List;
5. import java.util.Random;
6.
7. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
8.
9. import com.wy.object.Users;
10. import com.wy.object.entry.User;
11. import com.wy.object.server.IUserService;
12.
13. /**
14. * 调用UserService客户端
15. *
16. * @author Administrator
17. *
18. */
19. public class UserServiceClient {
20. public static void main(String[] args) {
21.
22. // 调用WebService
23. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
24. factory.setServiceClass(IUserService.class);
25. factory.setAddress("http://localhost:8080/WebServiceCXF/services/IUserService");
26. IUserService service = (IUserService) factory.create();
27.
28. //string
29. System.out.println("@@@@@@@@@@@@@Client getUserByName@@@@@@@@@@@@@@");
30. User user = service.getUserByName("Jack");
31. System.out.println("Client: "+ user);
32. user.setAddress("China-BeiJing");
33.
34. //object JavaBean
35. service.setUser(user);
36.
37. //传递集合的包装对象 List
38. System.out.println("#############Client getUsers###################");
39. Users users = service.getUsers(4);
40. System.out.println("Client: "+ users);
41. List<User> tempUsers = new ArrayList<User>();
42. for (User u : users.getUsers()) {
43. System.out.println("Client: "+ u);
44. u.setName("Jack" + new Random().nextInt(100));
45. u.setAddress("Chian-BeiJing#" + new Random().nextInt(100));
46. tempUsers.add(u);
47. }
48. users.getUsers().clear();
49. users.getUsers().addAll(tempUsers);
50.
51. service.setUsers(users);
52.
53. //传递集合的包装对象 Array
54. System.out.println("$$$$$$$$$$$$Client getUserArray$$$$$$$$$$$$$$$");
55. users = service.getUserArray(4);
56. User[] userArr = new User[4];
57. int i = 0;
58. for (User u : users.getUserArr()) {
59. System.out.println("Client: "+ u);
60. u.setName("Jack" + new Random().nextInt(100));
61. u.setAddress("Chian-ShenZhen#" + new Random().nextInt(100));
62. userArr[i] = u;
63. i++;
64. }
65. users.setUserArr(userArr);
66.
67. service.setUserArray(users);
68.
69. //传递集合的包装对象 Map
70. System.out.println("------------Client getUserMap----------------");
71. users = service.getUserMap();
72. System.out.println("Client: "+ users.getMaps());
73. users.getMaps().put("ABA", userArr[0]);
74. service.setUserMap(users);
75.
76. }
77. }