WebService|WebService CXF学习(复杂对象传递(List,Map))
转自: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 { 7private ArrayListuserList; // 不要用List类型 8private HashMap userMap; // 不要用Map类型 9private 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 10public Users getAllUsers(); 11 12public Users getUsersMap(); 13 14public 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 13public Users getAllUsers() { 14ArrayListlist = new ArrayList (); 15list.add(new UserInfo("vicky",23)); 16list.add(new UserInfo("ivy",26)); 17list.add(new UserInfo("simon",26)); 18list.add(new UserInfo("carol",29)); 19Users users = new Users(); 20users.setUserList(list); 21return users; 22} 23 24public Users getUsersMap() { 25HashMap map = new HashMap (); 26map.put(23, new UserInfo("vicky",23)); 27map.put(22, new UserInfo("caty",22)); 28map.put(24, new UserInfo("leah",24)); 29map.put(25, new UserInfo("kelly",25)); 30map.put(27, new UserInfo("ivy",27)); 31map.put(26, new UserInfo("simon",26)); 32map.put(29, new UserInfo("carol",29)); 33 34Users users = new Users(); 35users.setUserMap(map); 36return users; 37} 38 39public Users getUsersArray() { 40UserInfo[] userInfo = new UserInfo[5]; 41for(int i=0; i<5; i++){ 42UserInfo info = new UserInfo(); 43info.setUserAge(23+i); 44info.setUserName("Array"+(i+1)); 45userInfo[i] = info; 46} 47Users users = new Users(); 48users.setUserArray(userInfo); 49return 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 { 8public ServerTest(){ 9// 发布User服务接口 10Endpoint.publish("http://localhost:8090/userInfoServices", new UserServicesImpl()); 11} 12public static void main(String[] args) { 13// 启动服务 14new ServerTest(); 15System.out.println("Server ready..."); 16try { 17Thread.sleep(1000*300); //休眠五分分钟,便于测试 18} catch (InterruptedException e) { 19e.printStackTrace(); 20} 21System.out.println("Server exit..."); 22System.exit(0); 23} 24 }
【WebService|WebService CXF学习(复杂对象传递(List,Map))】第四步:创建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 { 10public static void main(String[] args) { 11//创建WebService客户端代理工厂 12JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); 13//注册WebService接口 14factory.setServiceClass(IUserServices.class); 15//设置WebService地址 16factory.setAddress("http://localhost:8090/userInfoServices"); 17IUserServices userServices = (IUserServices)factory.create(); 18System.out.println("invoke userinfo webservice..."); 19// 测试Map 20 //testMap(userServices); 21// 测试List 22 //testList(userServices); 23// 测试Array 24 //testArray(userServices); 25System.exit(0); 26} 27 28public static void testArray(IUserServices userServices){ 29Users users = userServices.getUsersArray(); 30if(users!=null){ 31UserInfo[] array = users.getUserArray(); 32for(UserInfo info:array){ 33System.out.println("UserName: "+info.getUserName()); 34System.out.println("UserAge : "+info.getUserAge()); 35} 36} 37} 38 39public static void testList(IUserServices userServices){ 40Users users = userServices.getAllUsers(); 41if(users!=null){ 42Listlist = users.getUserList(); 43for(UserInfo info:list){ 44System.out.println("UserName: "+info.getUserName()); 45System.out.println("UserAge : "+info.getUserAge()); 46} 47} 48} 49 50public static void testMap(IUserServices userServices){ 51Users users = userServices.getUsersMap(); 52if(users!=null){ 53UserInfo info = users.getUserMap().get(23); 54System.out.println("UserName: "+info.getUserName()); 55System.out.println("UserAge : "+info.getUserAge()); 56} 57} 58 }
第五步:运行服务端,验证webservices服务是否发布成功
第六步:运行客户端,验证是否成功调用webservices服务
注:在做webServices复杂对象传递时,返回值的类型不要用接口类型。例如(List 应该换成ArrayList ,Map应该换成HashMap)
推荐阅读
- 由浅入深理解AOP
- 继续努力,自主学习家庭Day135(20181015)
- python学习之|python学习之 实现QQ自动发送消息
- 一起来学习C语言的字符串转换函数
- 定制一套英文学习方案
- 漫画初学者如何学习漫画背景的透视画法(这篇教程请收藏好了!)
- 《深度倾听》第5天──「RIA学习力」便签输出第16期
- 如何更好的去学习
- 【韩语学习】(韩语随堂笔记整理)
- 焦点学习田源分享第267天《来访》