1.这里在上一篇的基础上增加一下难度,大致思路为:
- 完成的目标是,在服务端的接口中继续添加一个新的抽象方法,
List<Cat> getCatsByUser(User user);
- 这个方法是接受一个对象参数,然后返回一个装载有cat对象的list集合,
- 两个javabean
- 在实体类user中提供重写hashcode和equals比较的方法,当其中两个属性值相同是基说明两个属性相等
- 最后测试的结果的时候,当传入的user参数与web service传出的参数值相同是,则暴露相关的cat对象属性,相关的完整的服务端和客户端的代码上传在csdn中
package org.fkjava.cxf.entity;
public class Cat {
private Integer id;
private String name;
private String color;
public Cat() {
}
public Cat(Integer id, String name, String color) {
this.id = id;
this.name = name;
this.color = color;
}
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
package org.fkjava.cxf.entity;
public class User {
private Integer id;
private String name;
private String sex;
private Integer age;
private String address;
private String pass;
public User() {
}
public User(Integer id, String name, String sex, Integer age,
String address, String pass) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.address = address;
this.pass = pass;
}
@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;
}
@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 String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
- 这里说明一下这是web service所提供的业务逻辑的代码
package org.fkjava.cxf.ws.impl;
import java.awt.CardLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.fkjava.cxf.entity.Cat;
import org.fkjava.cxf.entity.User;
import org.fkjava.cxf.service.UserService;
public class UserServiceImpl implements UserService {
static Map<User, List<Cat>> catDb=new HashMap();
static {
List<Cat> catList1=new ArrayList<Cat>();
catList1.add(new Cat(1,"garfield1","zhengse1"));
catList1.add(new Cat(2,"机器猫1","白色1"));
catDb.put(new User(1,"周杰伦1","男1",40,"台湾1","pass1"), catList1);
List<Cat> catList2=new ArrayList<Cat>();
catList2.add(new Cat(3,"garfield2","zhengse2"));
catList2.add(new Cat(4,"机器猫2","白色2"));
catDb.put(new User(2,"周杰伦2","男2",40,"台湾2","pass2"), catList2);
List<Cat> catList3=new ArrayList<Cat>();
catList3.add(new Cat(5,"garfield3","zhengse3"));
catList3.add(new Cat(6,"机器猫3","白色3"));
catDb.put(new User(3,"周杰伦3","男",40,"台湾3","pass3"), catList3);
List<Cat> catList4=new ArrayList<Cat>();
catList4.add(new Cat(7,"garfield4","zhengse4"));
catList4.add(new Cat(8,"机器猫4","白色4"));
catDb.put(new User(4,"周杰伦4","男4",40,"台湾4","pass4"), catList4);
List<Cat> catList5=new ArrayList<Cat>();
catList5.add(new Cat(9,"garfield5","zhengse5"));
catList5.add(new Cat(10,"机器猫5","白色5"));
catDb.put(new User(5,"周杰伦5","男5",40,"台湾","pass5"), catList5);
}
@Override
public List<Cat> getCatsByUser(User user) {
return catDb.get(user);
}
}
package lee;
import javax.xml.ws.Endpoint;
import org.fkjava.cxf.ws.HelloWord;
import org.fkjava.cxf.ws.impl.HelloWorldImpl;
public class ServiceMain {
public static void main(String[] args) {
HelloWord hw=new HelloWorldImpl();
Endpoint.publish("http://192.168.6.134:9999/lyh", hw);
System.out.println("web service暴露成功!");
}
}
package lee
import java.util.List
import org.fkjava.cxf.ws.Cat
import org.fkjava.cxf.ws.HelloWord
import org.fkjava.cxf.ws.User
import org.fkjava.cxf.ws.impl.HelloWorldImpl
public class ClientMain {
public static void main(String[] args) {
HelloWorldImpl factory=new HelloWorldImpl()
//此处返回的只是远程调用web service的代理,远程服务端的服务器不能关闭
HelloWord hw = factory.getHelloWorldImplPort()
System.out.println(hw.sayHi("周杰伦"))
User user=new User()
user.setId(20)
user.setName("周杰伦1")
user.setAddress("北京")
user.setPass("pass1")
user.setSex("2")
List<Cat> cats=hw.getCatsByUser(user)
for(Cat cat:cats ){
System.out.println(cat.getName())
}
}
}