JSON

Lai18.com IT技术文章收藏夹

字号+ 作者:H5之家 来源:H5之家 2015-11-01 09:26 我要评论( )

json rpc 是一种以json为消息#26684;式的远程调用服务,它是一套允许运行在不同操作系统、不同环境的程序实现基于Internet过程调用的规范和一系列的实现。这种远

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

">
<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/aop/spring-aop.xsd ">



<bean

>

<property>

<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

">



<import resource="jsonrpc.xml"/>



<bean

>

</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>

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • json 相关技术文章

    json 相关技术文章

    2015-10-05 13:28

网友点评