zoukankan      html  css  js  c++  java
  • 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. 查看运行结果

  • 相关阅读:
    HAOI2018 奇怪的背包
    HAOI2018 苹果树
    骑士游戏
    飞飞侠
    奶牛排队
    寻找道路
    [USACO08JAN]牛大赛Cow Contest
    灾后重建
    [USACO09DEC]牛收费路径Cow Toll Paths
    萌萌哒
  • 原文地址:https://www.cnblogs.com/forever2h/p/7047831.html
Copyright © 2011-2022 走看看