1、 首先看看服务器端的代码所需要的JavaBean对象
package com.hoo.entity;
import java.io.Serializable;
/**
* function:User Entity
* @author hoojo
* @createDate Dec 16, 2010 10:20:02 PM
* @file User.java
* @package com.hoo.entity
* @project AxisWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class User implements Serializable {
private static final long serialVersionUID = 677484458789332877L;
private int id;
private String name;
private String email;
private String address;
//getter、setter
@Override
public String toString() {
return this.id + "#" + this.name + "#" + this.email + "#" + this.address;
}
}
下面的是集合传递Users,CXF直接传递集合对象会出现异常,用一个对象包装下就Ok了,不知道是什么原因。异常信息如下:
DefaultValidationEventHandler: [ERROR]: unexpected element(uri:"", local:"item")
org.apache.cxf.interceptor.Fault: Unmarshalling Error: unexpected element (uri:"", local:"item")
package com.hoo.entity;
import java.util.HashMap;
import java.util.List;
/**
* function: Users Entity
* @author hoojo
* @createDate 2011-3-18 上午09:27:31
* @file Users.java
* @package com.hoo.entity
* @project CXFWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class Users {
private List
private User[] userArr;
private HashMap
//getter、setter方法
}
2、 下面看看复杂对象传递的服务器端代码
package com.hoo.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.hoo.entity.User;
import com.hoo.entity.Users;
/**
* function: WebService传递复杂对象,如JavaBean、Array、List、Map等
* @author hoojo
* @createDate 2011-3-18 上午08:22:55
* @file ComplexUserService.java
* @package com.hoo.service
* @project CXFWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
@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
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
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;
}
}