在上篇博客中介绍了入门实例,并搭建的基本的框架和引入jar包,下面在原来的基础上学习webservce如何处理返回值是list集合。
一、服务端
1.建pojo类(Cat和User)
public class User {
private int id;
private String name;
private String pass;
private String address;
public User(){
}
public User(int id, String name, String pass, String address) {
super();
this.id = id;
this.name = name;
this.pass = pass;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((pass == null) ? 0 : pass.hashCode());
return result;
}
//只要name和pass的值和分别相等就能获取到对象
@Override
public 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 (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (pass == null) {
if (other.pass != null)
return false;
} else if (!pass.equals(other.pass))
return false;
return true;
}
}
public class Cat { private int id; private String name; private String color; public Cat(){} public Cat(int id, String name, String color) { super(); this.id = id; this.name = name; this.color = color; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
2.建service接口和实现类
public interface UserService {
//返回值类型是list
List<Cat> getCatsByUser(User user);
}
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,"garfield","橙色")); catList1.add(new Cat(2,"机器猫","蓝色")); catDb.put(new User(1,"sun","3322","花果山"), catList1); List<Cat> catList2 = new ArrayList<Cat>(); catList2.add(new Cat(3,"kitty","白色")); catList2.add(new Cat(4,"熊猫","黑白色")); catDb.put(new User(2,"zhu","4422","高老庄"), catList2); } //返回值类型是list @Override public List<Cat> getCatsByUser(User user) { return catDb.get(user); }}
3.在webservice组件中调用方法
@WebService //是扩展java类提供的
public interface HelloWorld {
List<Cat> getCatsByUser(User user);
}
@WebService(endpointInterface="com.tgb.cxf.ws.HelloWorld" ,serviceName="HelloWorldWs")public class HelloWorldWs implements HelloWorld { private UserService us = new UserServiceImpl(); @Override public List<Cat> getCatsByUser(User user) { //在实际的项目中web service组件自己并不会去实现业务功能 //它只是调用业务逻辑主键的方法来暴露web service return us.getCatsByUser(user); }}
4.发布项目
也就是执行服务器端的main方法
5.查看wsdl文档如下
对文档的解析以后的博客中会有讲解。
二、客户端
服务器端发布成功后,一定要执行下面的命令客户端才能更新
public class ClientMain {在客户端调用webservice暴露的接口,然后获得的值是list类型的,然后通过遍历获取对象的值。
/**
* @param args
*/
public static void main(String[] args) {
HelloWorldWs factory = new HelloWorldWs();
//此处返回的只是远程web service的代理
HelloWorld hw = factory.getHelloWorldWsPort();
System.out.println(hw.sayHi("孙悟空"));
User user = new User();
user.setId(30);
user.setName("sun");
user.setPass("3322");
List<Cat> catList = hw.getCatsByUser(user);
for (Cat cat : catList) {
System.out.println(cat.getName());
}
}
}
运行结果:
孙悟空,您好现在时间是:Thu Aug 13 12:05:02 CST 2015
garfield
机器猫