异构语言交互访问 解决方案及例子(一)
异构语言交互访问 解决方案及例子(一)
方案一
后台 c++实现 、前端java调用
开源库:后端jsonrpc库 前端:jsonrpc4j
方案二
采用webservice实现
后台 c++实现 、前端java调用
开源库:后端:gsoap 前端:cxf
jsonrpc4j 库测试
---》服务端
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.googlecode.jsonrpc4j.JsonRpcServer;
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 3638336826344504848L;
private JsonRpcServer rpcService = null;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
rpcService = new JsonRpcServer(new HelloWorldService(), HelloWorldService.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
rpcService.handle(req, resp);
}
}
public class HelloWorldService{
public String SayHello(){
String s = "hello!";
System.out.println(s);
return s;
}
}
---》客户端
import java.net.URL;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
public class JavaRpcTest {
/**
* @param args
* @throws Throwable
*/
public static void main(String[] args) throws Throwable {
JsonRpcHttpClient client = new JsonRpcHttpClient(new URL("http://127.0.0.1:8888/javaRpcTest/index.json"));
String properties = client.invoke("SayHello", null, String.class);
System.out.println(properties);
}
}