CXF-03:使用CXF处理 JavaBean 式的复合类型和 List 集合类型的形参和返回值

有了 CXF-01、CXF-02 的铺垫,我们开始更深一步的探索 CXF 的秘密,可以直接按照下面的代码进行更改:

         * 当形参、返回值的类型是String、基本数据类型时,CXF可以处理;
         * 当形参、返回值的类型是JavaBean式的复合类、List集合、数组等时,CXF可以处理
         * 还有一些像 Map、非JavaBean式的复合类,CXF不可以处理;

         * 代码有时比语言更犀利,在代码中前行,在代码中享受汲取的乐趣:

         

         * 1 . 增加一个形参、返回值的类型是JavaBean式的复合类、List集合、数组等的方法

import java.util.List;
import javax.jws.WebService;
import org.fjava.cxf.model.Food;
import org.fjava.cxf.model.User;
@WebService//J2EE文档里查看WebService
public interface HelloWorld {
	//返回的String类型
	String sayHello(String name);
	//这个方法的形参和返回值都是比较复杂的类型,User对象是没有的
	List<Food> getFoodsByUser(User user);
}
import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import org.fjava.cxf.model.Food;
import org.fjava.cxf.model.User;
import org.fjava.cxf.ws.HelloWorld;
import org.fjava.cxf.service.UserService;
import org.fjava.cxf.service.UserServiceImpl;
@WebService(endpointInterface="org.fjava.cxf.ws.HelloWorld", serviceName="HelloWorldWs")  
//endpointInterface=""表示实现的接口  serviceName是wsdl2java或其他语言后的文件名,可以不和HelloWorldWs相同
public class HelloWorldWs implements HelloWorld {
	@Override
	public String sayHello(String name) {
		return "Hello ! " + name + ",您好" + "现在时间是:" + new Date();
	}
	@Override
	public List<Food> getFoodsByUser(User user) {
		// 在实际的项目中,Web Service组件自己并不会去实现业务功能,交给业务逻辑组件去实现
		// 它只是调用业务逻辑组件的方法来暴露Web Service
		UserService userService = new UserServiceImpl();
		return userService.getFoodsByUser(user);
	}
}
         * 2 . 编写  JavaBean 在 User 里添加了构造方法
public class Food {
	private Integer id;
	private String name;
	private String describe;
	public Food() {
		super();
	}
	public Food(Integer id, String name, String describe) {
		super();
		this.id = id;
		this.name = name;
		this.describe = describe;
	}
	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 getDescribe() {
		return describe;
	}
	public void setDescribe(String describe) {
		this.describe = describe;
	}
}
public class User {
	private Integer id;
	private String name;
	private String tel;
	private String describe;
	public Integer getId() {
		return id;
	}
	public User() {
		super();
	}
	public User(Integer id, String name, String tel, String describe) {
		super();
		this.id = id;
		this.name = name;
		this.tel = tel;
		this.describe = describe;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getDescribe() {
		return describe;
	}
	public void setDescribe(String describe) {
		this.describe = describe;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((describe == null) ? 0 : describe.hashCode());
		result = prime * result + ((name == null) ? 0 : name.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 (describe == null) {
			if (other.describe != null)
				return false;
		} else if (!describe.equals(other.describe))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}	
}
         * 3 . 编写  Service  ,不写 Dao 层,用一个HashMap来模拟内存中的数据库
import java.util.List;
import org.fjava.cxf.model.Food;
import org.fjava.cxf.model.User;
public interface UserService {
	List<Food> getFoodsByUser(User user);
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.fjava.cxf.model.Food;
import org.fjava.cxf.model.User;
public class UserServiceImpl implements UserService {
	//为简单,不写Dao层,用一个HashMap来模拟内存中的数据库
	static Map<User, List<Food>> foodDb = new HashMap<>();
	static{
		List<Food> catList1 = new ArrayList<Food>();
		catList1.add(new Food(1 , "一个汉堡" , "是三层的,有夹层哦!"));
		catList1.add(new Food(2 , "火腿肠" , "这是章鱼哥从岸上偷运来的,据说很美味!"));
		foodDb.put(new User(1 , "海绵宝宝" , "123123123" , "开心的海绵宝宝!") , catList1);
		List<Food> catList2 = new ArrayList<Food>();
		catList2.add(new Food(3 , "蟹王汉堡" , "橙色,亮金色,我的宝贝,我的爱!"));
		catList2.add(new Food(4 , "海绵金币" , "吃着金币样的甜甜饼,想着海绵宝宝赚的钱被扣了,哈哈哈,爽气!"));
		foodDb.put(new User(2 , "蟹老板" , "321321321" , "哇!好有钱的蟹老板") , catList2);
	}
	@Override
	public List<Food> getFoodsByUser(User user) {
		return foodDb.get(user);
	}
}

         * 4 . 执行 wsdl2java 命令,重新生成客户端代码(执行详情见:CXF-02:使用CXF开发Web Service客户端http://blog.csdn.net/cheng_feng_xiao_zhan/article/details/52683987

         * 5 . 修改客户端执行代码(在CXF-2里你在 src 下新建 Package ,新建的 Class 那个执行类

import java.util.List;
import org.fjava.cxf.ws.Cat;
import org.fjava.cxf.ws.Food;
import org.fjava.cxf.ws.HelloWorld;
import org.fjava.cxf.ws.User;
import org.fjava.cxf.ws.impl.HelloWorldWs;
public class ClientMain {
	public static void main(String[] args) {
		//这是命令生成的类,该类的实例可当成工厂来使用
		HelloWorldWs factory = new HelloWorldWs();
		//无参的方法,返回的是远程Web Service服务端的代理,服务端不能关闭。
		HelloWorld helloWorld = factory.getHelloWorldWsPort();
		System.out.println(helloWorld.sayHello("海绵宝宝"));
		//调用 'wsdl2java' 命令重新生成java文件,新生成的bean里没有构造方法
		User user1  = new User();
		//在服务端里的bean里设置的相同的规则(在服务端User类里)是姓名相同则认定为同一人
		user1.setName("海绵宝宝");
		user1.setDescribe("开心的海绵宝宝!");
		List<Food> foodsByUser1 = helloWorld.getFoodsByUser(user1);
		for (Food food : foodsByUser1) {
			System.out.println(user1.getName() + "的食物:" + food.getName() + " " + food.getDescribe() + user1.getDescribe() + "!");
		}
		
		User user2  = new User();
		//在服务端里的bean里设置的相同的规则(在服务端User类里)是姓名相同则认定为同一人
		user2.setName("蟹老板");
		user2.setDescribe("哇!好有钱的蟹老板");
		List<Food> foodsByUser2 = helloWorld.getFoodsByUser(user2);
		for (Food food : foodsByUser2) {
			System.out.println(user1.getName() + "的食物:" + food.getName() + " " + food.getDescribe() + user2.getDescribe() + "!");
		}
	}
}

         * 6 . 运行成功:

Hello ! 海绵宝宝,您好现在时间是:Sat Oct 01 01:43:10 CST 2016
海绵宝宝的食物:一个汉堡 是三层的,有夹层哦!开心的海绵宝宝!!
海绵宝宝的食物:火腿肠 这是章鱼哥从岸上偷运来的,据说很美味!开心的海绵宝宝!!
海绵宝宝的食物:蟹王汉堡 橙色,亮金色,我的宝贝,我的爱!哇!好有钱的蟹老板!
海绵宝宝的食物:海绵金币 吃着金币样的甜甜饼,想着海绵宝宝赚的钱被扣了,哈哈哈,爽气!哇!好有钱的蟹老板!

注:如何设置CXF的服务端端口号:这是公司规定或个人设置,不可重复

import javax.xml.ws.Endpoint;
import org.fjava.cxf.ws.HelloWorld;
import org.fjava.cxf.ws.impl.HelloWorldWs;
//发布Web Service
public class ServiceMain {
	public static void main(String[] args){
		HelloWorld hw = new HelloWorldWs();
		//调用Endpoint的publish("本机地址","服务的提供者:一个Web Service对象")方法发布Web Service
		//如何设置cxf的服务端端口号:这是公司规定或个人设置,不可重复
		Endpoint.publish("http://192.168.1.3:6786/sayHello", hw);
		System.out.println("Web Service暴露成功!");
		//暴露成功后可以被任何平台的任何语言调用
		//检查调用地址http://192.168.*.*/sayHello?wsdl
	}
}

希望对你有帮助,祝你有一个好心情,加油!

若有错误、不全、可优化的点,欢迎纠正与补充;转载请注明出处!


猜你喜欢

转载自blog.csdn.net/cheng_feng_xiao_zhan/article/details/52713631
今日推荐