您的位置:首页 > Web前端 > JavaScript

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: