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

先抛出这一章的结论:
    1.当返回值,形参的类型是String,基本类型时,cxf能够轻松处理
    2.当返回值,形参的类型是javaBean复合类,List集合,数组等,cxf能够也轻松处理
    3.Map,非javaBean的复合类,cxf是处理不了的,后面会将到

接着上一张直接上代码
java

==================================服务端===========================web

//服务端接口代码

/**
 * @author xp
 * @Title: WebServiceDemo.java
 * @Package com.xp.cn
 * @Description: TODO
 * @date 2016年4月30日 下午8:27:44
 * @version V1.0  
 */
package com.xp.cn.ws;

import java.util.List;

import javax.jws.WebService;

import com.xp.cn.bean.Cat;
import com.xp.cn.bean.User;

/**
 * @author xp
 * @ClassName: WebServiceDemo
 * @Description: webService服务端
 * @date 2016年4月30日 下午8:27:44
 */
@WebService
public interface IWebServiceDemo {
	
	String sayHello(String name);
	
	List<Cat> getCatsByUser(User user);
	
}
//服务端接口代码实现类
/**
  @author xp
  @Title: WebServiceImpl.java
  @Package com.xp.cn
  @Description: TODO
  @date 2016年4月30日 下午8:39:22
  @version V1.0  
 **/
package com.xp.cn.ws.impl;

import java.util.Date;
import java.util.List;

import javax.jws.WebService;

import com.xp.cn.bean.Cat;
import com.xp.cn.bean.User;
import com.xp.cn.service.IuserService;
import com.xp.cn.service.impl.UserServiceImpl;
import com.xp.cn.ws.IWebServiceDemo;

/**
  @author xp
  @ClassName: WebServiceImpl
  @Description: TODO
  @date 2016年4月30日 下午8:39:22
 **/
@WebService(endpointInterface = "com.xp.cn.ws.IWebServiceDemo", 
			serviceName = "webServiceImpl")
public class WebServiceImpl implements IWebServiceDemo {
	
	/**
	 * 处理简单类型
	 * 1.形参和返回值都是简单类型
	 */
	@Override
	public String sayHello(String name) {
		return "你好" + name + new Date();
	}

	/**
	 * 处理复杂类型
	 * 2.形参和返回值都是复杂类型
	 */
	@Override
	public List<Cat> getCatsByUser(User user) {
		//webService本身并不会去实现业务逻辑功能
		//webService只是调用业务逻辑主键
		IuserService userService = new UserServiceImpl();
		return userService.getCatsByUser(user);
	}
}
//服务单javaBean,User
/**
 * @author xp
 * @Title: User.java
 * @Package com.xp.cn.bean
 * @Description: TODO
 * @date 2016年5月1日 下午12:07:58
 * @version V1.0  
 */
package com.xp.cn.bean;

/**
 * @author xp
 * @ClassName: User
 * @Description: TODO
 * @date 2016年5月1日 下午12:07:58
 *
 */
public class User {

	private Integer id;
	private String name;
	private String password;
	private String address;

	public User() {
		super();
	}

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

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * 只要name和password相等
	 * 咱们就认为该对象是同一个对象
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		return result;
	}

	/**
	 * 只要name和password相等
	 * 咱们就认为该对象是同一个对象
	 */
	@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 (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		return true;
	}

}
//服务端javaBean  Cat

/**
 * @author xp
 * @Title: Cat.java
 * @Package com.xp.cn.bean
 * @Description: TODO
 * @date 2016年5月1日 下午12:11:02
 * @version V1.0  
 */
package com.xp.cn.bean;

/**
 * @author xp
 * @ClassName: Cat
 * @Description: TODO
 * @date 2016年5月1日 下午12:11:02
 *
 */
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;
	}
}
//Service 接口组件
/**
 * @author xp
 * @Title: IuserService.java
 * @Package com.xp.cn.Service
 * @Description: TODO
 * @date 2016年5月1日 下午12:21:48
 * @version V1.0  
 */
package com.xp.cn.service;

import java.util.List;

import com.xp.cn.bean.Cat;
import com.xp.cn.bean.User;

/**
 * @author xp
 * @ClassName: IuserService
 * @Description: TODO
 * @date 2016年5月1日 下午12:21:48
 *
 */
public interface IuserService {
	
	List<Cat> getCatsByUser(User user);

}
//serviceimpl
/**
 * @author xp
 * @Title: UserServiceImpl.java
 * @Package com.xp.cn.Service
 * @Description: TODO
 * @date 2016年5月1日 下午12:22:21
 * @version V1.0  
 */
package com.xp.cn.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xp.cn.bean.Cat;
import com.xp.cn.bean.User;
import com.xp.cn.service.IuserService;

/**
 * @author xp
 * @ClassName: UserServiceImpl
 * @Description: TODO
 * @date 2016年5月1日 下午12:22:21
 *
 */
public class UserServiceImpl implements IuserService {
	
	//模拟内存数据库
	static Map<User, List<Cat>> catDb = new HashMap<User, List<Cat>>();
	
	static{
		List<Cat> catList  = new ArrayList<Cat>();
		catList.add(new Cat(1, "cat1", "black"));
		catList.add(new Cat(2, "cat2", "write"));
		catDb.put(new User(1, "jaychou", "jaychou", "taiwan"), catList);
		
		List<Cat> catList2  = new ArrayList<Cat>();
		catList2.add(new Cat(1, "cat3", "black"));
		catList2.add(new Cat(2, "cat3", "write"));
		catDb.put(new User(2, "xp", "xp", "shanghai"), catList2);
	}
	
	@Override
	public List<Cat> getCatsByUser(User user) {
		return catDb.get(user);
	}

}
//发布服务
/**
 * @author xp
 * @Title: WebServicePublish.java
 * @Package com.xp.cn
 * @Description: TODO
 * @date 2016年4月30日 下午9:06:49
 * @version V1.0  
 */
package com.xp.cn;

import javax.xml.ws.Endpoint;

import com.xp.cn.ws.IWebServiceDemo;
import com.xp.cn.ws.impl.WebServiceImpl;


/**
 * @author xp
 * @ClassName: WebServicePublish
 * @Description: TODO
 * @date 2016年4月30日 下午9:06:49
 *
 */
public class WebServicePublish {
	public static void main(String[] args) {
		IWebServiceDemo demo = new WebServiceImpl();
		//调用EndPoint发布服务
		Endpoint.publish("http://127.0.0.1/XXX", demo);
	}
}

好了,如今能够测试是否发布成功数据库

打开浏览器地址栏输入:http://127.0.0.1/XXX apache

出现下图说明发布成功数组


==================================客户端===========================浏览器

首先利用wsdl2java工具生成客户端代码ide

步骤章节和WebService简单开发 apache-cxf-3.1.6环境配置章节同样工具

调用webservice服务
测试

/**
 * @author xp
 * @Title: ClientMain.java
 * @Package com.xp.cn.test
 * @Description: TODO
 * @date 2016年5月1日 下午12:39:03
 * @version V1.0  
 */
package com.xp.cn.test;

import java.util.List;

import com.xp.cn.ws.Cat;
import com.xp.cn.ws.IWebServiceDemo;
import com.xp.cn.ws.User;
import com.xp.cn.ws.impl.WebServiceImpl;

/**
 * @author xp
 * @ClassName: ClientMain
 * @Description: TODO
 * @date 2016年5月1日 下午12:39:03
 *
 */
public class ClientMain {
	
	public static void main(String[] args) {
		
		//继承Service的类当成工厂使用
		WebServiceImpl webServiceImpl = new WebServiceImpl();
		//此处返回的只是远程webservice的代理
		IWebServiceDemo webService = webServiceImpl.getWebServiceImplPort();
		String sayHello = webService.sayHello("xp");
		System.out.println(sayHello);
		
		User user = new User();
		user.setName("jaychou");
		user.setPassword("jaychou");
		List<Cat> catsByUser = webService.getCatsByUser(user);
		
		for (Cat cat : catsByUser) {
			System.out.println(cat.getName());
			System.out.println(cat.getColor());
		}
	}
}

客户端和服务端包结构this

控制台打印信息以下

OK!

相关文章
相关标签/搜索