(四)CXF处理JavaBean以及复合类型

前面讲的是处理简单类型,今天这里来讲下CXF处理JavaBean以及复合类型,比如集合;

 

这里实例是客户端传一个JavaBean,服务器端返回集合类型;

 

在原来的项目实例基础上,我们先创建一个实体类User:

package com.wishwzp.entity;

/**
 * 用户实体类
 * @author Administrator
 *
 */
public class User {

    private Integer id; // 编号
    private String userName; // 用户名
    private String password; // 密码
     
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

 

再创建一个Role实体类:

package com.wishwzp.entity;

/**
 * 角色实体
 * @author Administrator
 *
 */
public class Role {
    
    private Integer id; // 编号
    private String roleName; // 角色名称  
     
    public Role() {
        super();
        // TODO Auto-generated constructor stub
    }
      
    public Role(Integer id, String roleName) {
        super();
        this.id = id;
        this.roleName = roleName;
    }
 
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
}

 

然后HelloWorld再加一个接口方法getRoleByUser,通过用户查找角色:

package com.wishwzp.webservice;

import java.util.List;

import javax.jws.WebService;

import com.wishwzp.entity.Role;
import com.wishwzp.entity.User;

@WebService
public interface HelloWorld {

    public String say(String str);
    
    public List<Role> getRoleByUser(User user);
}

 

然后HelloWorld接口实现类 HelloWorldImpl写下新增的方法的具体实现,我们这里写死,模拟下即可:

package com.wishwzp.webservice.impl;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import com.wishwzp.entity.Role;
import com.wishwzp.entity.User;
import com.wishwzp.webservice.HelloWorld;

@WebService
public class HelloWorldImpl implements HelloWorld{

    public String say(String str) {
        return "hello " + str;
    }

    public List<Role> getRoleByUser(User user) {
        // TODO Auto-generated method stub
         List<Role> roleList=new ArrayList<Role>();
            // 模拟 直接写死
            if(user!=null){
                if(user.getUserName().equals("wishwzp") && user.getPassword().equals("123456")){
                    roleList.add(new Role(1,"技术总监"));
                    roleList.add(new Role(2,"架构师"));
                }else if(user.getUserName().equals("jack") && user.getPassword().equals("123456")){
                    roleList.add(new Role(3,"程序员"));
                }
                return roleList;
            }else{
                return null;          
            }
    }

}

 

 

 

服务端其他地方不用动;

 

下面我们来处理下客户端,和前面讲的一样。我们用wsdl2java工具重新生成代码,这里就不再讲;

如:

我的地址是:D:\eclipse_workspace\WS_Client\src\main\java 你们的可能和我不一样:

然后我们进入dos,进入上面的本地硬盘地址,然后执行命令:wsdl2java http://192.168.0.110/helloWorld?wsdl

 

 

这里我看到,实体类,以及接口实现,代码都生成了。

 

我们改下Client类:

package com.wishwzp.webservice;

import java.util.List;

public class Client {
    
    public static void main(String[] args) {
        HelloWorldService service=new HelloWorldService();
        HelloWorld helloWorld=service.getHelloWorldPort();
        //System.out.println(helloWorld.say("wishwzp测试"));
        User user=new User();
        user.setUserName("jack");
        user.setPassword("123456");
        List<Role> roleList=helloWorld.getRoleByUser(user);
        for(Role role:roleList){
            System.out.println(role.getId()+","+role.getRoleName());
        }
    }

}

运行截图:

 

转载于:https://www.cnblogs.com/wishwzp/p/9916116.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值