CXF-02: 使用CXF处理JavaBean式的复合类型和List集合类型

时间:2023-03-10 02:56:51
CXF-02: 使用CXF处理JavaBean式的复合类型和List集合类型

CXF-02: 使用CXF处理JavaBean式的复合类型和List集合类型

Cat.java:

 package com.war3.ws.domain;

 public class Cat {

     private Integer id;
private String name;
private String color; public Cat() {
super();
} public Cat(Integer id, String name, String color) {
super();
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;
} }

User.java:

 package com.war3.ws.domain;

 public class User {

     private Integer id;
private String name;
private String pass;
private String address; public User() {
super();
} public User(Integer id, String name, String pass, String address) {
super();
this.id = id;
this.name = name;
this.pass = pass;
this.address = address;
} 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 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;
} @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;
} }

UserService.java:

 package com.war3.service;

 import java.util.List;

 import com.war3.ws.domain.Cat;
import com.war3.ws.domain.User; public interface UserService { List<Cat> getCatsByUser(User user);
}

UserServiceImpl.java:

 package com.war3.service.impl;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.war3.service.UserService;
import com.war3.ws.domain.Cat;
import com.war3.ws.domain.User; public class UserServiceImpl implements UserService { 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,"孙悟空","1111","花果山"), catList1); List<Cat> catList2 = new ArrayList<Cat>();
catList2.add(new Cat(3,"熊猫","黑白色"));
catList2.add(new Cat(4,"kitty","白色"));
catDb.put(new User(2,"猪八戒","2222","高老庄"), catList2);
}
@Override
public List<Cat> getCatsByUser(User user) {
return catDb.get(user);
} }

HelloWorld.java:

 package com.war3.ws;

 import java.util.List;

 import javax.jws.WebService;

 import com.war3.ws.domain.Cat;
import com.war3.ws.domain.User; @WebService
public interface HelloWorld { List<Cat> getCatsByUser(User user);
String sayHi(String name);
}

HelloWorldWS.java:

 package com.war3.ws.impl;

 import java.util.List;

 import javax.jws.WebService;

 import com.war3.service.UserService;
import com.war3.service.impl.UserServiceImpl;
import com.war3.ws.HelloWorld;
import com.war3.ws.domain.Cat;
import com.war3.ws.domain.User; @WebService(endpointInterface="com.war3.ws.HelloWorld")
public class HelloWorldWS implements HelloWorld{ @Override
public List<Cat> getCatsByUser(User user) {
UserService us = new UserServiceImpl();
return us.getCatsByUser(user);
} @Override
public String sayHi(String name) {
return "welcome to the google"+name;
} }

ServerMain.java:

 package com.war3.ws.server;

 import javax.xml.ws.Endpoint;

 import com.war3.ws.HelloWorld;
import com.war3.ws.impl.HelloWorldWS; public class ServerMain { public static void main(String[] args) {
HelloWorld hw = new HelloWorldWS();
Endpoint.publish("http://localhost:8080/hello", hw);
System.out.println("WebService暴露服务成功!");
}
}

运行主类,暴露服务成功!

CXF-02: 使用CXF处理JavaBean式的复合类型和List集合类型

CXF-02: 使用CXF处理JavaBean式的复合类型和List集合类型

我们写一个测试类ClientMain.java:

 package com.war3.ws.client;

 import java.util.List;

 import com.war3.ws.Cat;
import com.war3.ws.HelloWorld;
import com.war3.ws.User;
import com.war3.ws.impl.HelloWorldWSService; public class ClientMain { public static void main(String[] args) {
HelloWorldWSService factory = new HelloWorldWSService();
HelloWorld hw = factory.getHelloWorldWSPort();
System.out.println(hw.sayHi("tom")); User user = new User();
user.setName("孙悟空");
user.setPass("1111");
List<Cat> catList = hw.getCatsByUser(user);
for(Cat cat:catList){
System.out.println(cat.getName()+","+cat.getColor());
}
}
}

CXF-02: 使用CXF处理JavaBean式的复合类型和List集合类型