我是玩框架的新手,以前只使用PHP来实现节俭的客户端/服务器。
我想使用play实现节俭服务。
我应该将thrift生成的java文件放在播放框架目录结构中的什么位置,以便它们被类加载器拾取?我之前看到过一个为他们推荐构建jar的答案,但这似乎太多了-我开发时将调整接口,并且不希望增加jar的开销-再加上我对Java有点生锈而且从来没有真正学会如何做。
谢谢
最佳答案
这是 Play上的示例工作代码!框架1.2.3 使用 Thrift 0.7.0 :
服务器
// Play! Framework Server Controller
public class Application extends Controller {
public static void index() {
render();
}
public static void api() throws ServletException {
if (!request.contentType.contains("application/x-thrift")) {
response.setContentTypeIfNotSet("text/plain");
response.print("Unsupported Call");
return;
}
try {
response.setContentTypeIfNotSet("application/x-thrift");
ThriftServiceHandler handler = new ThriftServiceHandler();
Processor processor = new MyThriftService.Processor(handler);
TTransport transport = new TIOStreamTransport(request.body, response.out);
TProtocol inProtocol = new TBinaryProtocol(transport);
TProtocol outProtocol = new TBinaryProtocol(transport);
processor.process(inProtocol, outProtocol);
} catch (TException te) {
throw new ServletException(te);
}
}
}
客户
// Java Thrift Client Using HTTP Transport
public static void main(String[] args) throws InvalidRequestException, AuthenticationException, TException, UnavailableException {
THttpClient transport = new THttpClient("http://localhost:9000/application/api");
TProtocol protocol = new TBinaryProtocol(transport);
MyThriftService.Client client = new MyThriftService.Client(protocol);
client.RPCMethod("some string");
}