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

Cat.java:

 1 package com.war3.ws.domain;
 2 
 3 public class Cat {
 4 
 5     private Integer id;
 6     private String name;
 7     private String color;
 8     
 9     public Cat() {
10         super();
11     }
12     
13     public Cat(Integer id, String name, String color) {
14         super();
15         this.id = id;
16         this.name = name;
17         this.color = color;
18     }
19     
20     public Integer getId() {
21         return id;
22     }
23     public void setId(Integer id) {
24         this.id = id;
25     }
26     public String getName() {
27         return name;
28     }
29     public void setName(String name) {
30         this.name = name;
31     }
32     public String getColor() {
33         return color;
34     }
35     public void setColor(String color) {
36         this.color = color;
37     }
38     
39 }

User.java:

 1 package com.war3.ws.domain;
 2 
 3 public class User {
 4 
 5     private Integer id;
 6     private String name;
 7     private String pass;
 8     private String address;
 9     
10     public User() {
11         super();
12     }
13     
14     public User(Integer id, String name, String pass, String address) {
15         super();
16         this.id = id;
17         this.name = name;
18         this.pass = pass;
19         this.address = address;
20     }
21 
22     public Integer getId() {
23         return id;
24     }
25     public void setId(Integer id) {
26         this.id = id;
27     }
28     public String getName() {
29         return name;
30     }
31     public void setName(String name) {
32         this.name = name;
33     }
34     public String getPass() {
35         return pass;
36     }
37     public void setPass(String pass) {
38         this.pass = pass;
39     }
40     public String getAddress() {
41         return address;
42     }
43     public void setAddress(String address) {
44         this.address = address;
45     }
46     
47     @Override
48     public int hashCode() {
49         final int prime = 31;
50         int result = 1;
51         result = prime * result + ((name == null) ? 0 : name.hashCode());
52         result = prime * result + ((pass == null) ? 0 : pass.hashCode());
53         return result;
54     }
55     
56     @Override
57     public boolean equals(Object obj) {
58         if (this == obj)
59             return true;
60         if (obj == null)
61             return false;
62         if (getClass() != obj.getClass())
63             return false;
64         User other = (User) obj;
65         if (name == null) {
66             if (other.name != null)
67                 return false;
68         } else if (!name.equals(other.name))
69             return false;
70         if (pass == null) {
71             if (other.pass != null)
72                 return false;
73         } else if (!pass.equals(other.pass))
74             return false;
75         return true;
76     }
77     
78 }

UserService.java:

 1 package com.war3.service;
 2 
 3 import java.util.List;
 4 
 5 import com.war3.ws.domain.Cat;
 6 import com.war3.ws.domain.User;
 7 
 8 public interface UserService {
 9 
10     List<Cat> getCatsByUser(User user);
11 }

UserServiceImpl.java:

 1 package com.war3.service.impl;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import com.war3.service.UserService;
 9 import com.war3.ws.domain.Cat;
10 import com.war3.ws.domain.User;
11 
12 public class UserServiceImpl implements UserService {
13 
14     static Map<User,List<Cat>> catDb = new HashMap<User,List<Cat>>();
15     
16     static{
17         List<Cat> catList1 = new ArrayList<Cat>();
18         catList1.add(new Cat(1,"加菲猫","橙色"));
19         catList1.add(new Cat(2,"机器猫","蓝色"));
20         catDb.put(new User(1,"孙悟空","1111","花果山"), catList1);
21         
22         List<Cat> catList2 = new ArrayList<Cat>();
23         catList2.add(new Cat(3,"熊猫","黑白色"));
24         catList2.add(new Cat(4,"kitty","白色"));
25         catDb.put(new User(2,"猪八戒","2222","高老庄"), catList2);
26     }
27     @Override
28     public List<Cat> getCatsByUser(User user) {
29         return catDb.get(user);
30     }
31 
32 }

HelloWorld.java:

 1 package com.war3.ws;
 2 
 3 import java.util.List;
 4 
 5 import javax.jws.WebService;
 6 
 7 import com.war3.ws.domain.Cat;
 8 import com.war3.ws.domain.User;
 9 
10 @WebService
11 public interface HelloWorld {
12 
13     List<Cat> getCatsByUser(User user);
14     String sayHi(String name);
15 }

HelloWorldWS.java:

 1 package com.war3.ws.impl;
 2 
 3 import java.util.List;
 4 
 5 import javax.jws.WebService;
 6 
 7 import com.war3.service.UserService;
 8 import com.war3.service.impl.UserServiceImpl;
 9 import com.war3.ws.HelloWorld;
10 import com.war3.ws.domain.Cat;
11 import com.war3.ws.domain.User;
12 
13 @WebService(endpointInterface="com.war3.ws.HelloWorld")
14 public class HelloWorldWS implements HelloWorld{
15 
16     @Override
17     public List<Cat> getCatsByUser(User user) {
18         UserService us = new UserServiceImpl();
19         return us.getCatsByUser(user);
20     }
21 
22     @Override
23     public String sayHi(String name) {
24         return "welcome to the google"+name;
25     }
26 
27 }

ServerMain.java:

 1 package com.war3.ws.server;
 2 
 3 import javax.xml.ws.Endpoint;
 4 
 5 import com.war3.ws.HelloWorld;
 6 import com.war3.ws.impl.HelloWorldWS;
 7 
 8 public class ServerMain {
 9 
10     public static void main(String[] args) {
11         HelloWorld hw = new HelloWorldWS();
12         Endpoint.publish("http://localhost:8080/hello", hw);
13         System.out.println("WebService暴露服务成功!");
14     }
15 }

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

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

 1 package com.war3.ws.client;
 2 
 3 import java.util.List;
 4 
 5 import com.war3.ws.Cat;
 6 import com.war3.ws.HelloWorld;
 7 import com.war3.ws.User;
 8 import com.war3.ws.impl.HelloWorldWSService;
 9 
10 public class ClientMain {
11 
12     public static void main(String[] args) {
13         HelloWorldWSService factory = new HelloWorldWSService();
14         HelloWorld hw = factory.getHelloWorldWSPort();
15         System.out.println(hw.sayHi("tom"));
16         
17         User user = new User();
18         user.setName("孙悟空");
19         user.setPass("1111");
20         List<Cat> catList = hw.getCatsByUser(user);
21         for(Cat cat:catList){
22             System.out.println(cat.getName()+","+cat.getColor());
23         }
24     }
25 }

 

posted on 2015-11-29 02:16  confirmCname  阅读(259)  评论(0编辑  收藏  举报

导航