webservice 之 CXF处理javaBean以及复合类型

需求:客户端传一个javaBean,服务端返回集合类型。

1. 在helloWorld的基础上添加 User 实体类:

package com.wh.entity;

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

2. 在helloWorld的基础上添加 Role 实体类:

package com.wh.entity;

public class Role {
    private Integer id;
    private String 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;
    }
    public Role(Integer id, String roleName) {
        super();
        this.id = id;
        this.roleName = roleName;
    }
    public Role() {
        super();
    }
    
    
}

3. 在HelloWorld上添加 接口方法 getRoleByUser,通过用户查找角色

package com.wh.webservice;

import java.util.List;

import javax.jws.WebService;

import com.wh.entity.Role;
import com.wh.entity.User;
@WebService
public interface HelloWorld {
    public String say(String str);
    
    public List<Role> getRoleByUser(User user);
}

3. 在HelloWorldImpl上添加  getRoleByUser方法的具体实现(制造数据)

package com.wh.webservice.impl;

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

import javax.jws.WebService;
import javax.management.relation.RoleResult;

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

@WebService
public class HelloWorldImpl implements HelloWorld {
    public String say(String str) {
        return "Hello" + str;
    }

    public List<Role> getRoleByUser(User user) {
        List<Role> roleList =new ArrayList<Role>();
        if(user!=null){
            if(user.getUserName().equals("wh") && user.getPassword().equals("111")){
                roleList.add(new Role(1,"CEO"));
                roleList.add(new Role(2,"机构师"));
            }else if(user.getUserName().equals("ww") && user.getPassword().equals("111")){
                roleList.add(new Role(3,"项目经理"));
            }
            return roleList;
    }else {
        return null;
    }
    }
}

好了,服务端就ok了。

 

4. 客户端,继续用wsdl2java工具重新生成代码

5. 编写 Client.java

package com.wh.webservice;

import java.util.List;

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

6. 查看运行结果

 

posted on 2017-06-19 11:09  forever_2h  阅读(268)  评论(0编辑  收藏  举报

导航