转自:https://blog.csdn.net/z69183787/article/details/35988335
第一步:创建存储复杂对象的类(因为WebServices的复杂对象的传递,一定要借助第三方对象(即自定义对象)来实现)
1 package com.ws.model; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 6 public class Users { 7 private ArrayList<UserInfo> userList; // 不要用List类型 8 private HashMap<Integer, UserInfo> userMap; // 不要用Map类型 9 private UserInfo[] userArray; 10 // geter/seter 方法 11 }
第二步:创建WebServices的服务端接口和实现类
1 package com.ws.services; 2 3 import javax.jws.WebService; 4 import com.ws.model.UserInfo; 5 import com.ws.model.Users; 6 7 @WebService 8 public interface IUserServices { 9 10 public Users getAllUsers(); 11 12 public Users getUsersMap(); 13 14 public Users getUsersArray(); 15 16 }
1 package com.ws.services.impl; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import javax.jws.WebService; 6 import com.ws.model.UserInfo; 7 import com.ws.model.Users; 8 import com.ws.services.IUserServices; 9 10 @WebService 11 public class UserServicesImpl implements IUserServices { 12 13 public Users getAllUsers() { 14 ArrayList<UserInfo> list = new ArrayList<UserInfo>(); 15 list.add(new UserInfo("vicky",23)); 16 list.add(new UserInfo("ivy",26)); 17 list.add(new UserInfo("simon",26)); 18 list.add(new UserInfo("carol",29)); 19 Users users = new Users(); 20 users.setUserList(list); 21 return users; 22 } 23 24 public Users getUsersMap() { 25 HashMap<Integer, UserInfo> map = new HashMap<Integer, UserInfo>(); 26 map.put(23, new UserInfo("vicky",23)); 27 map.put(22, new UserInfo("caty",22)); 28 map.put(24, new UserInfo("leah",24)); 29 map.put(25, new UserInfo("kelly",25)); 30 map.put(27, new UserInfo("ivy",27)); 31 map.put(26, new UserInfo("simon",26)); 32 map.put(29, new UserInfo("carol",29)); 33 34 Users users = new Users(); 35 users.setUserMap(map); 36 return users; 37 } 38 39 public Users getUsersArray() { 40 UserInfo[] userInfo = new UserInfo[5]; 41 for(int i=0;i<5;i++){ 42 UserInfo info = new UserInfo(); 43 info.setUserAge(23+i); 44 info.setUserName("Array"+(i+1)); 45 userInfo[i] = info; 46 } 47 Users users = new Users(); 48 users.setUserArray(userInfo); 49 return users; 50 } 51 52 }
第三步:创建WebServices的服务端
1 package com.test; 2 3 import javax.xml.ws.Endpoint; 4 import org.apache.cxf.jaxws.JaxWsServerFactoryBean; 5 import com.ws.services.impl.UserServicesImpl; 6 7 public class ServerTest { 8 public ServerTest(){ 9 // 发布User服务接口 10 Endpoint.publish("http://localhost:8090/userInfoServices", new UserServicesImpl()); 11 } 12 public static void main(String[] args) { 13 // 启动服务 14 new ServerTest(); 15 System.out.println("Server ready..."); 16 try { 17 Thread.sleep(1000*300);//休眠五分分钟,便于测试 18 } catch (InterruptedException e) { 19 e.printStackTrace(); 20 } 21 System.out.println("Server exit..."); 22 System.exit(0); 23 } 24 }
第四步:创建WebServices的客户端,并测试
1、将服务端创建的复杂对象的类和接口copy到客户工程中,且路径要与服务端一致;
2、新建测试类进行测试
1 package com.ws.client; 2 3 import java.util.List; 4 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; 5 import com.ws.model.UserInfo; 6 import com.ws.model.Users; 7 import com.ws.server.IUserServices; 8 9 public class UserTest { 10 public static void main(String[] args) { 11 //创建WebService客户端代理工厂 12 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); 13 //注册WebService接口 14 factory.setServiceClass(IUserServices.class); 15 //设置WebService地址 16 factory.setAddress("http://localhost:8090/userInfoServices"); 17 IUserServices userServices = (IUserServices)factory.create(); 18 System.out.println("invoke userinfo webservice..."); 19 // 测试Map 20 // testMap(userServices); 21 // 测试List 22 // testList(userServices); 23 // 测试Array 24 // testArray(userServices); 25 System.exit(0); 26 } 27 28 public static void testArray(IUserServices userServices){ 29 Users users = userServices.getUsersArray(); 30 if(users!=null){ 31 UserInfo[] array = users.getUserArray(); 32 for(UserInfo info:array){ 33 System.out.println("UserName: "+info.getUserName()); 34 System.out.println("UserAge : "+info.getUserAge()); 35 } 36 } 37 } 38 39 public static void testList(IUserServices userServices){ 40 Users users = userServices.getAllUsers(); 41 if(users!=null){ 42 List<UserInfo> list = users.getUserList(); 43 for(UserInfo info:list){ 44 System.out.println("UserName: "+info.getUserName()); 45 System.out.println("UserAge : "+info.getUserAge()); 46 } 47 } 48 } 49 50 public static void testMap(IUserServices userServices){ 51 Users users = userServices.getUsersMap(); 52 if(users!=null){ 53 UserInfo info = users.getUserMap().get(23); 54 System.out.println("UserName: "+info.getUserName()); 55 System.out.println("UserAge : "+info.getUserAge()); 56 } 57 } 58 }
第五步:运行服务端,验证webservices服务是否发布成功
第六步:运行客户端,验证是否成功调用webservices服务
注:在做webServices复杂对象传递时,返回值的类型不要用接口类型。例如(List 应该换成ArrayList ,Map应该换成HashMap)