WebService处理复杂对象List Map(jdk api jws)
XFire不用这么做,Apache CXF 只需要指定集合中具体类型,使用jdk中的web service api 才需如此
?
?
转载于:http://blog.csdn.net/chen_yu_ting/article/details/6606494
?
第一步:创建存储复杂对象的类(因为WebServices的复杂对象的传递,一定要借助第三方对象(即自定义对象)来实现)
package com.ws.client;import java.util.List;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import com.ws.model.UserInfo;import com.ws.model.Users;import com.ws.server.IUserServices;public class UserTest {public static void main(String[] args) {//创建WebService客户端代理工厂 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); //注册WebService接口 factory.setServiceClass(IUserServices.class); //设置WebService地址 factory.setAddress("http://localhost:8090/userInfoServices"); IUserServices userServices = (IUserServices)factory.create(); System.out.println("invoke userinfo webservice...");// 测试Map//testMap(userServices);// 测试List//testList(userServices);// 测试Array//testArray(userServices);System.exit(0); } public static void testArray(IUserServices userServices){Users users = userServices.getUsersArray();if(users!=null){UserInfo[] array = users.getUserArray();for(UserInfo info:array){System.out.println("UserName: "+info.getUserName());System.out.println("UserAge : "+info.getUserAge());}}}public static void testList(IUserServices userServices){Users users = userServices.getAllUsers();if(users!=null){List<UserInfo> list = users.getUserList();for(UserInfo info:list){System.out.println("UserName: "+info.getUserName());System.out.println("UserAge : "+info.getUserAge());}}}public static void testMap(IUserServices userServices){Users users = userServices.getUsersMap();if(users!=null){UserInfo info = users.getUserMap().get(23);System.out.println("UserName: "+info.getUserName());System.out.println("UserAge : "+info.getUserAge());}}}第五步:运行服务端,验证webservices服务是否发布成功
第六步:运行客户端,验证是否成功调用webservices服务
注:在做webServices复杂对象传递时,返回值的类型不要用接口类型。例如(List 应该换成ArrayList ,Map应该换成HashMap)