`
hanqunfeng
  • 浏览: 1548851 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Thrift--JSClient

 
阅读更多

thrift提供了基于jquery--ajax的客户端调用方式,返回json数据。

生成js代码使用如下命令:

 

thrift --gen js Contact.thrift

这样会在gen-js目录下生成两个js文件:Contact_types.js,ContactManager.js,将这两个js文件拷贝到项目中。

同时需要导入thrift.js(thrift-0.9.1\lib\js目录下)和JQuery.js(1.5以上的版本)。

 

 

编写一个jsp页面thriftJs.jsp,用于展示客户端的调用方法:

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="resources/js/thrift.js"></script>
<script type="text/javascript" src="resources/js/jquery-1.7.2.min.js"></script>

<!-- ContactManager -->
<script type="text/javascript" src="resources/js/Contact_types.js"></script>
<script type="text/javascript" src="resources/js/ContactManager.js"></script>

<script type="text/javascript">
	$(function() {
		try {
			var path = "<%=request.getContextPath()%>";
			//使用ajax,不能跨域访问,这里传入一个type=json参数
			var transport = new Thrift.Transport(path + "/contractManageServletProxy.do?type=json");
			var protocol = new Thrift.Protocol(transport);
			var client = new ContactManagerClient(protocol);
			var result_GetString = client.getAll(); //json格式
			//alert(result_GetString);
			$("#thriftjs").html(result_GetString[0].id);
		} catch (e) {
			alert("出错鸟:" + e.message);
		}
	});
</script>

</head>
<body>
	thrift js result:
	<div id="thriftjs"></div>
</body>
</html>

 

 服务端依然使用http://hanqunfeng.iteye.com/blog/1946999中的servlet代理,只不过这里为了使其能够支持json类型,对代理进行了改写:

@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		TTransport inTransport = null;
		TTransport outTransport = null;
		TProtocolFactory inProtocolFactory = null;
		TProtocolFactory outProtocolFactory = null;
		String local_type = request.getParameter("type");

		try {
			
			if(StringUtils.hasText(local_type)){
				protocolType = local_type;
			}
			
			if ("json".equals(protocolType)) {//支持json协议
				inProtocolFactory = new TJSONProtocol.Factory();
				outProtocolFactory = new TJSONProtocol.Factory();
				response.setContentType("application/json");
			} else {
				inProtocolFactory = new TCompactProtocol.Factory();
				outProtocolFactory = new TCompactProtocol.Factory();
				response.setContentType("application/x-thrift");
			}
			

			if (null != this.customHeaders) {
				for (Map.Entry<String, String> header : this.customHeaders) {
					response.addHeader(header.getKey(), header.getValue());
				}
			}
			InputStream in = request.getInputStream();
			OutputStream out = response.getOutputStream();

			TTransport transport = new TIOStreamTransport(in, out);
			inTransport = transport;
			outTransport = transport;

			TProtocol inProtocol = inProtocolFactory.getProtocol(inTransport);
			TProtocol outProtocol = outProtocolFactory
					.getProtocol(outTransport);

			processor.process(inProtocol, outProtocol);
			out.flush();
		} catch (TException te) {
			throw new ServletException(te);
		}
	}

 

 

 

分享到:
评论

相关推荐

    thrift-ios-nodejs-example:使用 Thrift 连接到 NodeJs 服务器的示例 iOS 客户端

    thrift -o server --gen js:node thrift/messaging-service.thrift 您通常可能不想对生成的代码进行版本控制,而是在构建/编译时生成它。 该代码包含在此处以快速参考它的外观。 运行服务器 运行服务器: cd ...

    thrift javascript example

    4. `client.js`: 客户端代码,导入Thrift库,创建服务代理,调用服务端方法。 5. 可能还有测试文件或者示例数据,用于验证客户端和服务端的正确性。 在实际操作中,你需要按照以下步骤进行: 1. **安装Thrift**: ...

    thrift的使用介绍

    $response = $client-&gt;sayHello('World'); echo $response; ``` **4. Python服务器实现** Python可以作为Thrift服务的实现语言。`PythonClientTest.py`可能不仅包含客户端测试,还可能包含服务端的简单实现: ```...

    C# Thrift测试

    thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Go,Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, ...

    node-impala:使用Apache Thrift的Impala的节点客户端

    const client = createClient ( ) ; client . connect ( { host : '127.0.0.1' , port : 21000 , resultType : 'json-array' } ) ; client . query ( 'SELECT column_name FROM table_name' ) . then ( result =...

    kvclient-js:用于 node.js 的 kvclient

    要工作,该模块需要使用代理服务器,该服务器使用 thrift 协议在该模块和 Oracle NoSQL 数据库存储之间转换网络活动。 该代理是用纯 Java 编写的,可以在客户端和 Oracle NoSQL 数据库存储可通过网络访问的任何...

    node.js中RPC(远程过程调用)的实现原理介绍

    RPC(远程过程调用)是一种通信机制,允许在分布式系统中的一个进程调用另一个进程中...通过了解这个基本的RPC实现原理,我们可以更好地理解和使用其他Node.js RPC库,比如gRPC、thrift等,以构建更复杂的分布式应用。

    apps:go语言(golang)学习应用

    Go-Study ( Go Language / golang ) apps 这是学习golang中用到的各种代码...go_thrift/ hello/ http/ html_template/ html_template_js/ server_redis/ server_redis_pool/ server_wiki/ redis/ consistent/ consiste

Global site tag (gtag.js) - Google Analytics