页面树结构
转至元数据结尾
转至元数据起始

BusinessViewService WebService 示例


下面以Java环境为例,介绍如何在应用系统中调用Smartbi WebService BusinessViewService API。


1、通过wsimport生成Java的WebService客户端实现


wsimport是在JDK(1.6或以上)的bin目录下的一个exe文件(Windows版),
主要功能是根据服务端发布的wsdl文件生成客户端存根及框架,负责 与Web Service 服务器通信,并在将其封装成实例,客户端可以直接使用,就像使用本地实例一样。对Java而言,wsimport帮助程序员生存调用web service所需要的客户端类文件.java和.class。
要提醒指出的是,wsimport可以用于非Java的服务器端,如:服务器端也许是C# 编写的web service。


wsimport命令的参数说明:

  • -d 生成客户端执行类的class文件的存放目录
  • -s 生成客户端执行类的源文件的存放目录
  • -p 定义生成类的包名
    更多参数说明请执行:wsimport -help


打开Windows命令行程序,通过以下命令生成UserManagerServiceBusinessViewService的WebService客户端实现源码(在本示例文档中UserManagerService主要用于登录):

 


2、新建Java工程SmartbiWSDemo,添加上一步生成的WebService客户端实现的Java文件以及所依赖的jar:


3、核心示例代码如下:

package demo.ws;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.ws.BindingProvider;
import smartbi.net.sf.json.JSONArray;
import smartbi.net.sf.json.JSONObject;
import demo.ws.businessview.ArrayOfArrayOfString;
import demo.ws.businessview.ArrayOfString;
import demo.ws.businessview.BusinessViewService;
import demo.ws.businessview.BusinessViewServicePortType;
import demo.ws.businessview.ViewMetaData;
import demo.ws.usermanager.UserManagerService;
import demo.ws.usermanager.UserManagerServicePortType;


public class BusinessViewServiceWSDemo {
	private static final String KEY_RESPONSE_HEADERS = "javax.xml.ws.http.response.headers";
	private static final String KEY_REQUEST_HEADERS = "javax.xml.ws.http.request.headers";
	private static List<String> cookieHeaders = null;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BusinessViewServiceWSDemo demo = new BusinessViewServiceWSDemo();
		demo.run();
	}
	/**
	 * 测试BusinessViewService WebService
	 */
	private void run() {
		if (!login()) {
			System.out.println("登录失败,请检查"连接url、用户名、密码"是否正确!");
			return;
		}
		try {
			testBusinessViewService();
		} finally {
			logout();
		}
	}
	/**
	 * 登录服务器
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private boolean login() {
		String user = "admin";
		String password = "manager";
		//
		UserManagerServicePortType userServicePort = new UserManagerService()
				.getUserManagerServiceHttpSoap11Endpoint();
		BindingProvider bp = (BindingProvider) userServicePort;
		// bp.getRequestContext().put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
		boolean success = userServicePort.login(user, password);
		if (success) {
			Map<String, List<String>> headers = (Map<String, List<String>>) bp.getResponseContext()
					.get(KEY_RESPONSE_HEADERS);
			// Retrieve the cookie from the response message
			cookieHeaders = headers.get("Set-Cookie");
		}
		return success;
	}
	/*
	 * uses HTTP cookies to establish credentials with the server
	 */
	@SuppressWarnings("unchecked")
	private void retrieveCookie(Map<String, Object> requestContext) {
		Map<String, List<String>> headers = (Map<String, List<String>>) requestContext
				.get(KEY_REQUEST_HEADERS);
		if (headers == null) {
			headers = new HashMap<String, List<String>>();
			requestContext.put(KEY_REQUEST_HEADERS, headers);
		}
		headers.put("cookie", cookieHeaders);
	}
	/**
	 * 使用服务对象
	 */
	private void testBusinessViewService() {
		BusinessViewServicePortType bizViewServicePort = new BusinessViewService()
				.getBusinessViewServiceHttpSoap11Endpoint();
		BindingProvider bp = (BindingProvider) bizViewServicePort;
		// bp.getRequestContext().put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
		retrieveCookie(bp.getRequestContext());
		// 业务查询ID(对应示例库中的"根目录\功能演示\查询定义\原生SQL查询\带参数的原生SQL")
		String bizViewId = "I2c94907127855da201278df551b62602";
		// 参数ID
		String paramId = "OutputParameter.I2c94907127855da201278df551b62602.产品名称参数_列表对话框";
		// 参数真实值
		String paramValue = "牛奶,花生,饼干,猪肉,汽水";
		// 参数显示值
		// String paramDisplayValue = paramValue;
		//
		JSONArray paramsArr = new JSONArray();
		JSONObject paramObj = new JSONObject();
		paramObj.put("id", paramId);
		paramObj.put("value", paramValue);
		// paramObj.put("displayValue", paramDisplayValue);
		paramsArr.put(paramObj);
		String paramsJsonArrStr = paramsArr.toString();
		//
		// 每页行数
		int rowsPerPage = 2;
		// 是否获取总行数
		boolean getTotalRows = true;
		// 打开查询
		ViewMetaData ret = bizViewServicePort.openLoadDataView(bizViewId, paramsJsonArrStr,
				rowsPerPage, getTotalRows);
		String loadDataClientId = ret.getClientId();
		int totalRows = ret.getTotalRowCount();
		List<String> fieldNames = ret.getFieldNames();
		// List<String> fieldTypes = ret.getFieldTypes();
		//
		int pages = totalRows / rowsPerPage + (totalRows % rowsPerPage == 0 ? 0 : 1);
		System.out.println("共" + totalRows + "行,每页" + rowsPerPage + "行,共" + pages + "页:");
		StringBuilder buf = new StringBuilder();
		buf.append("|\t");
		for (String name : fieldNames) {
			buf.append(name).append("\t");
		}
		buf.append("|");
		System.out.println("-----------------------------------------");
		System.out.println(buf.toString());
		// 按分页读取数据
		for (int pageNum = 0; pageNum < pages; pageNum++) {
			System.out.println("---第" + (pageNum + 1) + "页---------------------------------");
			ArrayOfArrayOfString dataList = bizViewServicePort.loadViewData(loadDataClientId,
					pageNum);
			if (dataList != null && dataList.getArray().size() > 0) {
				List<ArrayOfString> cellDatas = dataList.getArray();
				for (ArrayOfString cellDataArr : cellDatas) {
					buf.setLength(0);
					buf.append("|\t");
					for (String data : cellDataArr.getArray()) {
						buf.append(data).append("\t");
					}
					buf.append("|");
					System.out.println(buf.toString());
				}
			}
		}
		System.out.println("-----------------------------------------");
		// 关闭查询
		bizViewServicePort.closeLoadDataView(loadDataClientId);
	}
	/**
	 * 注销
	 */
	private boolean logout() {
		UserManagerServicePortType userServicePort = new UserManagerService()
				.getUserManagerServiceHttpSoap11Endpoint();
		BindingProvider bp = (BindingProvider) userServicePort;
		retrieveCookie(bp.getRequestContext());
		return userServicePort.logout();
	}
}

 

4、示例运行效果:

 

5、示例源码(Eclipse)工程

SmartbiWSDemo_Eclipse_Project.rar

 

 

  • 无标签