前面介绍的都是传递简单的字符串,现在开始介绍传递复杂类型的对象。如JavaBean、Array、List、Map等。
1、首先看看服务器端的代码所需要的JavaBean对象
package com.iflytek.entity; import java.io.Serializable; /** * @author xdwang * * @create 2013-7-25 下午7:32:33 * * @email:[email protected] * * @description 实体 * */ public class User implements Serializable { private int id; private String name; private String email; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return this.id + "#" + this.name + "#" + this.email + "#" + this.address; } }
下面的是集合传递Users,CXF直接传递集合对象会出现异常,用一个对象包装下就Ok了
package com.iflytek.entity; import java.util.HashMap; import java.util.List; /** * @author xdwang * * @create 2013-7-25 下午7:36:02 * * @email:[email protected] * * @description * */ public class Users { private List<User> users; private User[] userArr; private HashMap<String, User> maps; public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } public User[] getUserArr() { return userArr; } public void setUserArr(User[] userArr) { this.userArr = userArr; } public HashMap<String, User> getMaps() { return maps; } public void setMaps(HashMap<String, User> maps) { this.maps = maps; } }
2、下面看看复杂对象传递的服务器端代码
package com.iflytek.service; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import javax.jws.WebParam; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; import com.iflytek.entity.User; import com.iflytek.entity.Users; /** * @author xdwang * * @create 2013-7-25 下午7:34:56 * * @email:[email protected] * * @description WebService传递复杂对象,如JavaBean、Array、List、Map等 * */ @WebService @SOAPBinding(style = Style.RPC) @SuppressWarnings("deprecation") public class ComplexUserService { public User getUserByName(@WebParam(name = "name") String name) { User user = new User(); user.setId(new Date().getSeconds()); user.setName(name); user.setAddress("china"); user.setEmail(name + "@iflytek.com"); return user; } public void setUser(User user) { System.out.println("============Server setUser==========="); System.out.println("setUser:" + user); } public Users getUsers(int i) { List<User> users = new ArrayList<User>(); for (int j = 0; j <= i; j++) { User user = new User(); user.setId(new Date().getSeconds()); user.setName("xdwang=" + j); user.setAddress("china"); user.setEmail("xdwang" + j + "@iflytek.com"); users.add(user); } Users u = new Users(); u.setUsers(users); return u; } public void setUsers(Users users) { System.out.println("============Server setUsers==========="); for (User u : users.getUsers()) { System.out.println("setUsers:" + u); } } public Users getUserArray(int i) { User[] users = new User[i]; for (int j = 0; j < i; j++) { User user = new User(); user.setId(new Date().getSeconds()); user.setName("xdwang=" + j); user.setAddress("china"); user.setEmail("xdwang" + j + "@iflytek.com"); users[j] = user; } Users u = new Users(); u.setUserArr(users); return u; } public void setUserArray(Users users) { System.out.println("============Server setUserArray==========="); for (User u : users.getUserArr()) { System.out.println("setUserArray:" + u); } } public void setUserMap(Users maps) { System.out.println("============Server setUserMap==========="); System.out.println("setUserMap:" + maps.getMaps()); } public Users getUserMap() { HashMap<String, User> users = new HashMap<String, User>(); User user = new User(); user.setId(new Date().getSeconds()); user.setName("xdwang="); user.setAddress("china="); user.setEmail("[email protected]"); users.put("A", user); user = new User(); user.setId(new Date().getSeconds()); user.setName("xiaoming"); user.setAddress("china$$"); user.setEmail("[email protected]"); users.put("B", user); Users u = new Users(); u.setMaps(users); return u; } }
3、发布WebService的代码
package com.iflytek.service.deploy; import com.iflytek.service.ComplexUserService; /** * @author xdwang * * @create 2013-7-25 下午7:40:04 * * @email:[email protected] * * @description 发布ComplexUserService服务 * */ public class DeployComplexUserService { public static void main(String[] args) throws InterruptedException { String address = "http://localhost:9000/complexUser"; DeployUtils.deployService(address, new ComplexUserService()); Thread.sleep(1000 * 60 * 20); System.exit(0); System.out.println("Server exit "); } }
DeployUtils.java
package com.iflytek.service.deploy; import javax.xml.ws.Endpoint; /** * @author xdwang * * @create 2013-7-25 下午7:40:51 * * @email:[email protected] * * @description 发布WebService工具类 * */ public class DeployUtils { public static void deployService(String address, Object service) { System.out.println("Server start ……"); Endpoint.publish(address, service); } }
4、通过发布后的地址的http://localhost:9000/complexUser?wsdl; wsdl中的内容定制你的客户端调用WebService的接口,当然你也可以让服务器端实现一个接口。
package com.iflytek.service; import javax.jws.WebParam; import javax.jws.WebService; import com.iflytek.entity.User; import com.iflytek.entity.Users; /** * @author xdwang * * @create 2013-7-25 下午7:41:42 * * @email:[email protected] * * @description import javax.jws.WebParam;import javax.jws.WebService; * */ @WebService public interface IComplexUserService { public User getUserByName(@WebParam(name = "name") String name); public void setUser(User user); public Users getUsers(int i); public void setUsers(Users users); public Users getUserArray(int i); public void setUserArray(Users users); public void setUserMap(Users maps); public Users getUserMap(); }
5、客户端调用代码
package com.iflytek.client; import java.util.ArrayList; import java.util.List; import java.util.Random; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.iflytek.entity.User; import com.iflytek.entity.Users; import com.iflytek.service.IComplexUserService; /** * @author xdwang * * @create 2013-7-25 下午7:42:35 * * @email:[email protected] * * @description 调用ComplexUserService客户端 * */ public class ComplexUserServiceClient { public static void main(String[] args) { // 调用WebService JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(IComplexUserService.class); factory.setAddress("http://localhost:9000/complexUser"); IComplexUserService service = (IComplexUserService) factory.create(); System.out.println("=============Client getUserByName=============="); User user = service.getUserByName("xdwang"); System.out.println(user); user.setAddress("China-Anhui"); service.setUser(user); System.out.println("=============Client getUsers=============="); Users users = service.getUsers(4); System.out.println(users); List<User> tempUsers = new ArrayList<User>(); for (User u : users.getUsers()) { System.out.println(u); u.setName("xdwang" + new Random().nextInt(100)); u.setAddress("Chian-Anhui=" + new Random().nextInt(100)); tempUsers.add(u); } users.getUsers().clear(); users.getUsers().addAll(tempUsers); service.setUsers(users); System.out.println("=============Client getUserArray=============="); users = service.getUserArray(4); User[] userArr = new User[4]; int i = 0; for (User u : users.getUserArr()) { System.out.println(u); u.setName("xdwang" + new Random().nextInt(100)); u.setAddress("Chian-Anhui=" + new Random().nextInt(100)); userArr[i] = u; i++; } users.setUserArr(userArr); service.setUserArray(users); System.out.println("==================Client getUserMap==============="); users = service.getUserMap(); System.out.println(users.getMaps()); users.getMaps().put("ABA", userArr[0]); service.setUserMap(users); } }
运行结果:
Server start …… 2013-7-25 19:44:31 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://service.iflytek.com/}ComplexUserServiceService from class com.iflytek.service.ComplexUserService 2013-7-25 19:44:32 org.apache.cxf.endpoint.ServerImpl initDestination 信息: Setting the server's publish address to be http://localhost:9000/complexUser 2013-7-25 19:44:32 org.eclipse.jetty.server.Server doStart 信息: jetty-8.1.7.v20120910 2013-7-25 19:44:32 org.eclipse.jetty.server.AbstractConnector doStart 信息: Started SelectChannelConnector@localhost:9000 2013-7-25 19:44:32 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL 信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}Discovery from WSDL: classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl 2013-7-25 19:44:32 org.apache.cxf.endpoint.ServerImpl initDestination 信息: Setting the server's publish address to be soap.udp://239.255.255.250:3702 2013-7-25 19:44:32 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}DiscoveryProxy from class org.apache.cxf.jaxws.support.DummyImpl ============Server setUser=========== setUser:40#xdwang#[email protected]#China-Anhui ============Server setUsers=========== setUsers:40#xdwang94#[email protected]#Chian-Anhui=91 setUsers:40#xdwang77#[email protected]#Chian-Anhui=1 setUsers:40#xdwang13#[email protected]#Chian-Anhui=43 setUsers:40#xdwang45#[email protected]#Chian-Anhui=10 setUsers:40#xdwang69#[email protected]#Chian-Anhui=70 ============Server setUserArray=========== setUserArray:40#xdwang53#[email protected]#Chian-Anhui=20 setUserArray:40#xdwang59#[email protected]#Chian-Anhui=73 setUserArray:40#xdwang30#[email protected]#Chian-Anhui=70 setUserArray:40#xdwang30#[email protected]#Chian-Anhui=33 ============Server setUserMap=========== setUserMap:{ABA=40#xdwang53#[email protected]#Chian-Anhui=20, A=40#xdwang=#[email protected]#china=, B=40#xiaoming#[email protected]#china$$}