json rpc
2015-10-22 10:38
483 查看
json rpc 是一种以json为消息格式的远程调用服务,它是一套允许运行在不同操作系统、不同环境的程序实现基于Internet过程调用的规范和一系列的实现。这种远程过程调用可以使用http作为传输协议,也可以使用其它传输协议,传输的内容是json消息体。
json rpc 和
xmlrpc相比具有很多优点。首先xmlrpc是以xml作为消息格式,xml具有体积大,格式复杂,传输占用带宽。程序对xml的解析也比较复杂,并且耗费较多服务器资源。json相比xml体积小巧,并且解析相对容易很多。
JSON:JavaScript Object Notation,JavaScript对象的一种字面量描述格式,是一种轻量级的数据交换格式。
RPC:Remote procedure call,远程过程(函数、方法)调用。
jsonrpc 依赖 jsonrpc-1.0.jar 文件,以及一个 js文件(jsonrpc.js)
举例:
(1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
<!--注册配置的Bean对象-->
org.jsonrpc.service.RegistServiceToJsonrpcBridgeListener
</listener-class>
</listener>
<servlet>
<servlet-name>JSONRPCServlet</servlet-name>
<servlet-class>
com.metaparadigm.jsonrpc.JSONRPCServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JSONRPCServlet</servlet-name>
<url-pattern>/JSON-RPC</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file> index.html </welcome-file>
</welcome-file-list>
</web-app>
(2)jsonrpc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
<bean name="jsonrpcServiceConfigurer"
class="org.jsonrpc.service.spring.JsonrpcServiceConfigurer">
<property name="services">
<map>
<entry key="helloWorld">
<ref bean="helloWorld" />
</entry>
</map>
</property>
</bean>
</beans>
(3)applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd ">
<import resource="jsonrpc.xml"/>
<bean id="helloWorld"
class="com.logcd.service.impl.HelloWorldImpl">
</bean>
</beans>
(4)HelloWorld.java和HelloWorldImpl.java
public interface HelloWorld {
public String sayHelloWorld(String name);
}
public class HelloWorldImpl implements HelloWorld{
public String sayHelloWorld(String name) {
return "Hello World! hello, " + name;
}
}
(5)调用
<script type="text/javascript" src="script/jsonrpc.js"></script><script>
var jsonrpc = null;
//初始化JSONRpcClient对象
jsonrpc = new JSONRpcClient("JSON-RPC");
function jsonrpcHello(){
var name = "阿花!";
var result = jsonrpc.helloWorld.sayHelloWorld(resultCallback,name);
}
function resultCallback(result, e){
alert("The server replied: " + result);
}
</script>
json rpc 和
xmlrpc相比具有很多优点。首先xmlrpc是以xml作为消息格式,xml具有体积大,格式复杂,传输占用带宽。程序对xml的解析也比较复杂,并且耗费较多服务器资源。json相比xml体积小巧,并且解析相对容易很多。
JSON:JavaScript Object Notation,JavaScript对象的一种字面量描述格式,是一种轻量级的数据交换格式。
RPC:Remote procedure call,远程过程(函数、方法)调用。
jsonrpc 依赖 jsonrpc-1.0.jar 文件,以及一个 js文件(jsonrpc.js)
举例:
(1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
<!--注册配置的Bean对象-->
org.jsonrpc.service.RegistServiceToJsonrpcBridgeListener
</listener-class>
</listener>
<servlet>
<servlet-name>JSONRPCServlet</servlet-name>
<servlet-class>
com.metaparadigm.jsonrpc.JSONRPCServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JSONRPCServlet</servlet-name>
<url-pattern>/JSON-RPC</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file> index.html </welcome-file>
</welcome-file-list>
</web-app>
(2)jsonrpc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
<bean name="jsonrpcServiceConfigurer"
class="org.jsonrpc.service.spring.JsonrpcServiceConfigurer">
<property name="services">
<map>
<entry key="helloWorld">
<ref bean="helloWorld" />
</entry>
</map>
</property>
</bean>
</beans>
(3)applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd ">
<import resource="jsonrpc.xml"/>
<bean id="helloWorld"
class="com.logcd.service.impl.HelloWorldImpl">
</bean>
</beans>
(4)HelloWorld.java和HelloWorldImpl.java
public interface HelloWorld {
public String sayHelloWorld(String name);
}
public class HelloWorldImpl implements HelloWorld{
public String sayHelloWorld(String name) {
return "Hello World! hello, " + name;
}
}
(5)调用
<script type="text/javascript" src="script/jsonrpc.js"></script><script>
var jsonrpc = null;
//初始化JSONRpcClient对象
jsonrpc = new JSONRpcClient("JSON-RPC");
function jsonrpcHello(){
var name = "阿花!";
var result = jsonrpc.helloWorld.sayHelloWorld(resultCallback,name);
}
function resultCallback(result, e){
alert("The server replied: " + result);
}
</script>
相关文章推荐
- javascript中this实例及理解
- 【JavaScript】利用滚动事件window.onscroll与position:fixed写兼容IE6的回到顶部组件
- JS设置cookie
- jsp-jsp中EL表达式无效
- js中对按键的监听
- js中获取URL中指定的查询字符串
- c#与JavaScript实现对用户名、密码进行RSA非对称加密
- js中,加号扩展
- JS+CSS实现大气清新的滑动菜单效果代码
- seajs学习(7)----文本插件
- seajs学习(6)----配置
- seajs学些(5)----模块的加载启动
- seajs学习(3)----模块标识
- seajs学习(2)----CMD 模块定义规范
- JS+CSS实现仿msn风格选项卡效果代码
- seajs学习(1)----什么是系统
- js 验证码 倒计时60秒
- js闭包
- js检测访问设备的类型
- chrome Sources选项卡 设置JavaScript事件断点