CXF学习02---处理JavaBean类型与List的集合类型

来源:互联网 发布:mac桌面东西没了 编辑:程序博客网 时间:2024/06/05 02:03

整体框架与上一篇文章一样,只不过在Web Service接口中新添加了一个方法,List<Cat> getCatsByUser(User user);

用来处理JavaBean的参数与List的返回值。

新添加两个JavaBean,

package com.onyas.ws.cxf.domain;public class Cat {private Integer id;private String color;private String name;public Cat() {}public Cat(Integer id, String color, String name) {super();this.id = id;this.color = color;this.name = name;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

package com.onyas.ws.cxf.domain;public class User {private Integer id;private String username;private String password;private String address;public User() {}public User(Integer id, String username, String password, String address) {super();this.id = id;this.username = username;this.password = password;this.address = address;}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;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result+ ((password == null) ? 0 : password.hashCode());result = prime * result+ ((username == null) ? 0 : username.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;User other = (User) obj;if (password == null) {if (other.password != null)return false;} else if (!password.equals(other.password))return false;if (username == null) {if (other.username != null)return false;} else if (!username.equals(other.username))return false;return true;}}


在Web Service实现类中加入以下方法

@Overridepublic List<Cat> getCatsByUser(User user) {//在实际的项目中,Web Service组件自已并不会去实现业务功能,//它只是调用业务逻辑组件的方法来暴露Web Service。UserService us = new UserServiceImpl();return us.getCatsByUser(user);}

因此还要新建一个业务接口

package com.onyas.ws.cxf.service;import java.util.List;import com.onyas.ws.cxf.domain.Cat;import com.onyas.ws.cxf.domain.User;public interface UserService {List<Cat> getCatsByUser(User user);}

还有业务实现类

package com.onyas.ws.cxf.service.impl;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.onyas.ws.cxf.domain.Cat;import com.onyas.ws.cxf.domain.User;import com.onyas.ws.cxf.service.UserService;public class UserServiceImpl implements UserService {// 用一个HashMap来模拟内存中的数据库static Map<User, List<Cat>> catDb = new HashMap<User, List<Cat>>();static {List<Cat> catList1 = new ArrayList<Cat>();catList1.add(new Cat(1, "红色", "红眼猫"));catList1.add(new Cat(2, "蓝色", "机器猫"));catDb.put(new User(1, "小明", "123", "上海"), catList1);List<Cat> catList2 = new ArrayList<Cat>();catList2.add(new Cat(3, "黑色", "加菲"));catList2.add(new Cat(4, "白色", "小白"));catDb.put(new User(2, "小红", "123", "北京"), catList2);}@Overridepublic List<Cat> getCatsByUser(User user) {return catDb.get(user);}}

其它地方不变,然后可以发布Web Service了。

重新用wsdl2java生成客户端代码,客户端架构也一样,

如下所示:

package onyas;import java.util.List;import com.onyas.ws.cxf.Cat;import com.onyas.ws.cxf.HelloWorld;import com.onyas.ws.cxf.User;import com.onyas.ws.cxf.impl.HelloWorldWs;public class ClentMain {public static void main(String[] args) {HelloWorldWs factory = new HelloWorldWs();//此处返回的只是远程Web Service的代理HelloWorld hw = factory.getHelloWorldWsPort();System.out.println(hw.sayHi("Test"));User user = new User();user.setUsername("小明");user.setPassword("123");List<Cat> cats = hw.getCatsByUser(user);for(Cat cat:cats){System.out.println(cat.getName());}}}

运行客户端代码

显示内容如下:

Hello TestFri May 02 14:24:00 CST 2014
红眼猫
机器猫


代码下载

0 1