文章摘抄至 http://www.cnblogs.com/hoojo/archive/2011/03/30/1999523.html 前面介绍的都是传递简单的字符串,现在开始介绍传递复杂类型的对象。如JavaBean、Array、List、Map等。 1、 首先看看服务器端的
文章摘抄至 http://www.cnblogs.com/hoojo/archive/2011/03/30/1999523.html
前面介绍的都是传递简单的字符串,现在开始介绍传递复杂类型的对象。如JavaBean、Array、List、Map等。
1、 首先看看服务器端的代码所需要的JavaBean对象
package model; import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 677484458789332877L; private int id; private String name; private String email; private String address; @Override public String toString() { return this.id + "#" + this.name + "#" + this.email + "#" + this.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; } public static long getSerialversionuid() { return serialVersionUID; } }
package model; import java.util.HashMap; import java.util.List; /** * <b>function:</b> Users Entity */ 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; } }
下面的是集合传递Users,CXF直接传递集合对象会出现异常,用一个对象包装下就Ok了,不知道是什么原因。异常信息如下:
DefaultValidationEventHandler: [ERROR]: unexpected element(uri:"", local:"item")
org.apache.cxf.interceptor.Fault: Unmarshalling Error: unexpected element (uri:"", local:"item")
2、 下面看看复杂对象传递的服务器端代码
package 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 model.User; import model.Users; /** * <b>function:</b> 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 + "@hoo.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("jack#" + j); user.setAddress("china"); user.setEmail("jack" + j + "@hoo.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("jack#" + j); user.setAddress("china"); user.setEmail("jack" + j + "@hoo.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("jack#"); user.setAddress("china#"); user.setEmail("jack@hoo.com"); users.put("A", user); user = new User(); user.setId(new Date().getSeconds()); user.setName("tom"); user.setAddress("china$$"); user.setEmail("tom@hoo.com"); users.put("B", user); Users u = new Users(); u.setMaps(users); return u; } }
3、 发布WebService的代码
package deploy; import service.ComplexUserService; /** * <b>function:</b> 发布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 "); } }
package deploy; import javax.xml.ws.Endpoint; /** * <b>function:</b> 发布WebService工具类 */ public final class DeployUtils { /** * <b>function:</b>发布WebService * @author hoojo */ 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 service; import javax.jws.WebParam; import javax.jws.WebService; import model.User; import model.Users; /** * <b>function:</b>定制客户端请求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 test; import java.util.ArrayList; import java.util.List; import java.util.Random; import model.User; import model.Users; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import service.IComplexUserService; /** * <b>function:</b>调用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("hoojo"); System.out.println(user); user.setAddress("China-Guangzhou"); 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("hoojo" + new Random().nextInt(100)); u.setAddress("Chian-GuangZhou#" + 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("hoojo" + new Random().nextInt(100)); u.setAddress("Chian-ShenZhen#" + 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); } }