使用CXF开发WebService程序的总结(四):基于bean的客户端和服务端代码的编写

1. 在原服务端项目 ws_server中添加两个bean

1.1 添加两个类  User 和 Clazz  
package com.lonely.pojo;

public class User {

	private Integer id;

	private String username;

	private Integer classId;

	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 Integer getClassId() {
		return classId;
	}

	public void setClassId(Integer classId) {
		this.classId = classId;
	}

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}

	public User(Integer id, String username, Integer classId) {
		super();
		this.id = id;
		this.username = username;
		this.classId = classId;
	}

}
package com.lonely.pojo;

public class Clazz {

	private Integer classId;

	private String className;

	public Integer getClassId() {
		return classId;
	}

	public void setClassId(Integer classId) {
		this.classId = classId;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public Clazz() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Clazz(Integer classId, String className) {
		super();
		this.classId = classId;
		this.className = className;
	}

}
1.2 在原服务接口 HelloWs中添加一个 方法
/**
 * 根据班级id 查看该班级下的所有学生
 * 
 * @param clazz
 * @return
 */
public List<User> findUsersByClassId(Clazz clazz);
1.3 在实现类中模拟实现
@Override
	public List<User> findUsersByClassId(Clazz clazz) {
		// 这里模拟数据库查询
		if (clazz != null && clazz.getClassId() != null) {
			List<User> users = new ArrayList<>();
			if (clazz.getClassId() == 1) {
				users.add(new User(1, "独孤", 1));
				users.add(new User(2, "呵呵", 1));
			} else if (clazz.getClassId() == 2) {
				users.add(new User(3, "啧啧", 2));
			}
			return users;
		}
		return null;
	}
1.4 重新发布


2. 客户端调用

2.1 重新生成客户端代码 使用命令  wsdl2java +地址+?wsdl
2.2 测试
package com.lonely.client;

import java.util.List;

import com.lonely.server.Clazz;
import com.lonely.server.HelloWs;
import com.lonely.server.HelloWsService;
import com.lonely.server.User;

public class HelloInvoking {

	public static void main(String[] args) {
		HelloWsService helloWsService = new HelloWsService();
		HelloWs helloWs = helloWsService.getHelloWsPort();
		// System.out.println(helloWs.sayHelloWs("dugu"));

		Clazz clazz = new Clazz();
		clazz.setClassId(1);
		List<User> list = helloWs.findUsersByClassId(clazz);
		for (User user : list) {
			System.out.println(user.getUsername() + ":" + user.getClassId());
		}

	}
}
2.3 查看结果,至此 基于 普通bean的调用完成





posted @ 2018-01-02 12:41  风中追风_lonely  阅读(143)  评论(0编辑  收藏  举报