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;}}
@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
- CXF学习02---处理JavaBean类型与List的集合类型
- CXF-03:使用CXF处理 JavaBean 式的复合类型和 List 集合类型的形参和返回值
- CXF处理JavaBean以及复合类型
- CXF处理JavaBean以及复合类型
- CXF学习-处理CXF不能处理的类型
- 【WebService】CXF处理javaBean等复合类型以及Map等复杂类型的数据
- 【WebService】CXF处理javaBean等复合类型以及Map等复杂类型的数据
- 【WebService】4.使用CXF处理JavaBean和List集合
- 4.Web Service CXF处理JavaBean以及复合类型
- CXF-05:使用CXF处理返回值的类型为Map、非JavaBean式的复合类
- CXF--处理复杂类型
- 处理Map、非javabean式的复合类等CXF无法自动转化的类型 (3)
- 1.当形参,返回值类型不是JavaBean式的复合类,Map时,CXF无法处理:服务器接口
- 5.当形参,返回值类型不是JavaBean式的复合类,Map时,CXF无法处理:服务端domain
- 6.当形参,返回值类型不是JavaBean式的复合类,Map时,CXF无法处理:服务端发布
- 7.当形参,返回值类型不是JavaBean式的复合类,Map时,CXF无法处理:转换器1
- 8.当形参,返回值类型不是JavaBean式的复合类,Map时,CXF无法处理:转换器2
- cxf无法处理类型解决方法
- Codeforces Unlucky Ticket 题解
- IC总线应用下的EEPROM
- 黑马程序员---.NET\C#J基础扫盲日记
- Spiral Matrix II
- Asp.Net Mvc 4 使用Ajax 遇到的问题
- CXF学习02---处理JavaBean类型与List的集合类型
- js控制图片自适应屏幕
- NGUI学习之路(2)Scroll View 简洁版【NGUI 2.6.3】
- Java卡环境介绍
- 黑马程序员—java异常
- 深入探讨在集群环境中使用 EhCache 缓存系统
- java Swing 之文本域控件的使用
- C++引用的本质与修改引用的方法
- split image使用方法