本项目示例和TestThrift项目(https://github.com/KingWang93/TestThrift) 是配套的,属于前端代码,可以配合TestThrift项目完成前后端http请求过程,下面将整个流程(前后端)展示如下:
https://thrift.apache.org/docs/install/windows
https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/shared.thrift
https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/tutorial.thrift
thrift-0.10.0.exe -r --gen js --gen java tutorial.thrift
http://www.apache.org/dyn/closer.cgi?path=/thrift/0.10.0/thrift-0.10.0.tar.gz cmd进入\thrift-0.10.0\lib\java,ant即可,完成后jar包在build子目录,把libthrift-0.10.0.jar和lib子目录下的jar包都拷贝出来,添加到上面测试工程依赖
这个可以可以从http://www.apache.org/dyn/closer.cgi?path=/thrift/0.10.0/thrift-0.10.0.tar.gz 下载下来的文件目录下(thrift-0.10.0\tutorial\js\src下)有个httpd.java文件,这个利用了httpcore的jar包。实现创建httpserver的功能。
https://stackoverflow.com/questions/8153832/xmlhttprequest-changes-post-to-option
https://stackoverflow.com/questions/25727306/request-header-field-access-control-allow-headers-is-not-allowed-by-access-contr
js部分需要支持options请求里,在response里加上response.addHeader("Access-Control-Allow-Origin", "*"),改造下httpd.java
https://github.com/LukeOwncloud/ThriftJavaJavascriptDemo/blob/master/index.html
http://127.0.0.1:8088/service 把这个html放在一个Tomcat服务下访问
上面讲解的是利用httpd.java实现thrift远程调用的后端功能。 其实在TestThrift项目下还利用vertx实现了创建httpserver的功能,具体参看后端代码。