cxf 怎么传递object 对象

 我来答
huanglenzhi
2016-01-15 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517184
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
1、 首先看看服务器端的代码所需要的JavaBean对象

package com.hoo.entity;

import java.io.Serializable;

/**
* <b>function:</b>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;

/**
* <b>function:</b> 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<User> users;
private User[] userArr;
private HashMap<String, User> maps;

//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;

/**
* <b>function:</b> 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<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;
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式