四、CXF WebService中传递复杂类型对象

转载 2015年07月09日 15:01:40


前面介绍的都是传递简单的字符串,现在开始介绍传递复杂类型的对象。如JavaBean、Array、List、Map等。

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

3、 发布WebService的代码

package com.hoo.service.deploy;
 
import com.hoo.service.ComplexUserService;
 
/**
 * <b>function:</b> 发布ComplexUserService服务
 * @author hoojo
 * @createDate 2011-3-18 上午08:28:57
 * @file DeployComplexUserService.java
 * @package com.hoo.service.deploy
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
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.hoo.service.deploy;
 
import javax.xml.ws.Endpoint;
 
/**
 * <b>function:</b> 发布WebService工具类
 * @author hoojo
 * @createDate Mar 17, 2011 8:14:05 PM
 * @file DeployUtils.java
 * @package com.hoo.service.deploy
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
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 com.hoo.service;
 
import javax.jws.WebParam;
import javax.jws.WebService;
import com.hoo.entity.User;
import com.hoo.entity.Users;
 
/**
 * <b>function:</b>定制客户端请求WebService所需要的接口
 * @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
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.hoo.client;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.hoo.entity.User;
import com.hoo.entity.Users;
import com.hoo.service.IComplexUserService;
 
/**
 * <b>function:</b>调用ComplexUserService客户端
 * @author hoojo
 * @createDate 2011-3-18 上午08:37:11
 * @file ComplexUserServiceClient.java
 * @package com.hoo.client
 * @project CXFWebService
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
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);
    }
}

6、 运行后结果如下

控制台客户端结果:

2011-3-18 10:31:11 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.hoo.com/}IComplexUserServiceService from class com.hoo.service.IComplexUserService
#############Client getUserByName##############
12#hoojo#hoojo@hoo.com#china
#############Client getUsers##############
com.hoo.entity.Users@1148603
12#jack#0#jack0@hoo.com#china
12#jack#1#jack1@hoo.com#china
12#jack#2#jack2@hoo.com#china
12#jack#3#jack3@hoo.com#china
12#jack#4#jack4@hoo.com#china
#############Client getUserArray##############
12#jack#0#jack0@hoo.com#china
12#jack#1#jack1@hoo.com#china
12#jack#2#jack2@hoo.com#china
12#jack#3#jack3@hoo.com#china
##################Client getUserMap###############
{A=12#jack##jack@hoo.com#china#, B=12#tom#tom@hoo.com#china$$}

服务器端结果:

Server start ……
2011-3-18 10:31:03 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.hoo.com/}ComplexUserServiceService from class com.hoo.service.ComplexUserService
2011-3-18 10:31:03 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:9000/complexUser
2011-03-18 10:31:03.906:INFO::jetty-7.2.2.v20101205
2011-03-18 10:31:04.046:INFO::Started SelectChannelConnector@localhost:9000
############Server setUser###########
setUser:12#hoojo#hoojo@hoo.com#China-Guangzhou
############Server setUsers###########
setUsers:12#hoojo67#jack0@hoo.com#Chian-GuangZhou#94
setUsers:12#hoojo95#jack1@hoo.com#Chian-GuangZhou#27
setUsers:12#hoojo29#jack2@hoo.com#Chian-GuangZhou#0
setUsers:12#hoojo94#jack3@hoo.com#Chian-GuangZhou#70
setUsers:12#hoojo49#jack4@hoo.com#Chian-GuangZhou#92
############Server setUserArray###########
setUserArray:12#hoojo24#jack0@hoo.com#Chian-ShenZhen#30
setUserArray:12#hoojo57#jack1@hoo.com#Chian-ShenZhen#81
setUserArray:12#hoojo84#jack2@hoo.com#Chian-ShenZhen#41
setUserArray:12#hoojo88#jack3@hoo.com#Chian-ShenZhen#79
############Server setUserMap###########
setUserMap:{ABA=12#hoojo24#jack0@hoo.com#Chian-ShenZhen#30, A=12#jack##jack@hoo.com#china#, B=12#tom#tom@hoo.com#china$$}

四、CXF WebService中传递复杂类型对象

前面介绍的都是传递简单的字符串,现在开始介绍传递复杂类型的对象。如JavaBean、Array、List、Map等。 1、 首先看看服务器端的代码所需要的JavaBean对象 pack...

CXF WebService中传递复杂类型对象

1、 首先看看服务器端的代码所需要的JavaBean对象 [java] view plain copy  print? package com.hoo.e...

WebService CXF学习(进阶篇2):复杂对象传递(List,Map)

前面讲到了JavaBean对象的传递,这一节我们就CXF框架复杂对象(List,Map)的传递进行讲解。 第一步:创建存储复杂对象的类(因为WebServices的复杂对象的传递,一定要借助第三方对象...

WebService CXF学习(进阶篇2):复杂对象传递(List,Map)

前面讲到了JavaBean对象的传递,这一节我们就CXF框架复杂对象(List,Map)的传递进行讲解。  第一步:创建存储复杂对象的类(因为WebServices的复杂对象的传递,一定要借助第三方...

cxf-webservice复杂类型

  • 2013年04月03日 16:16
  • 9.55MB
  • 下载

CXF webservice JAXB 处理复杂数据类型方法

假定有一个复杂类型如下public class Client { private Integer idclientinfo; private String clientname; private...

复杂对象类型的WebService

  • 2011年12月18日 15:19
  • 1.09MB
  • 下载

【WebService】CXF处理javaBean等复合类型以及Map等复杂类型的数据

在实际中除了传入一些基本的参数以外,还需要传入一些类似于javaBean等复合类型,或者Map等复杂类型的数据,这一篇博文主要来写两个demo,模拟一下CXF处理这类数据的过程。 1. CXF处理j...
  • eson_15
  • eson_15
  • 2016年07月30日 18:14
  • 6200

【WebService】CXF处理javaBean等复合类型以及Map等复杂类型的数据

WebService系列文章:  【WebService】带你走进webservice的世界  【WebService】自定义WebService服务及其调用  【WebService】wsdl配置详...

三、 复杂对象类型的WebService

1、这次我们编写复杂点的WebService方法,返回的数据是我们定义属性带getter、setter方法JavaBean,一维数组、二维数组等。 看代码: import java.io...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:四、CXF WebService中传递复杂类型对象
举报原因:
原因补充:

(最多只允许输入30个字)