博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Web服务实现方案四:JSON-RPC简介
阅读量:2353 次
发布时间:2019-05-10

本文共 4866 字,大约阅读时间需要 16 分钟。

 

参考:https://zh.wikipedia.org/wiki/XML-RPC

 

JSON-RPC是一种 中的编码。这是一个非常简单的协议,(非常类似于),只定义数据类型和命令的屈指可数。JSON-RPC允许通知(发送到不需要响应的服务器数据)和用于多个呼叫将被发送到可被回答无序服务器。

 

历史

描述 过时的
1.0 2005年
1.1 WD 增加了命名参数,增加了特定的错误代码,并添加了内省功能。 2006-08-07
1.1 Alt键 替代建议1.1 WD。 2007-05-06
1.1对象规格 替代建议1.1 WD / 1.1ALT。 2007-07-30
1.2 本文档的修订后更名为2.0。 2007-12-27
2.0 2009-05-24
2.0(Revised-) 2010-03-26

使用

JSON-RPC的工作原理是发送给实施这一协议的服务器发送请求。在这种情况下,客户机通常是软件意图调用远程系统的一个方法。多个输入参数可以被传递到作为数组或对象的远程方法,而该方法本身可以返回多个输出数据为好。(这取决于所实现的版本)。

远程方法是通过使用发送请求到远程服务调用或套接字(从2.0版)。当使用HTTP,所述可以被定义为应用/ JSON

所有的传输类型的单个对象,使用JSON序列化。请求是由远程系统所提供的特定方法的调用。它必须包含三个特定的属性:

  • 方法 -该方法的名称的字符串被调用。
  • PARAMS -对象的数组作为参数来定义的方法传递。
  • 编号 -的任何类型的,它被用来以匹配它答复请求的响应的值。

请求的接收器必须对所有接收到的请求的有效响应回复。的响应必须包含以下提到的属性。

  • 结果 -通过调用方法返回的数据。如果在调用方法时发生错误,则此值必须为空。
  • 错误 -如果在调用方法的错误,否则指定的错误代码
  • 编号 -这是响应请求的ID。

由于有在需要甚至期望没有反应的情况下,被引入的通知。通知是类似于除了ID,这是不是必要的,因为没有任何反应会返回一个请求。在这种情况下的id属性应被删去(版本2.0)或为(1.0版)。

 

Examples

In these examples, --> denotes data sent to a service (request), while <-- denotes data coming from a service. (Although <-- often is called response in client–server computing, depending on the JSON-RPC version it does not necessarily imply answer to a request).

Version 1.0

A simple request and response:

--> {
"method": "echo", "params": ["Hello JSON-RPC"], "id": 1}<-- {
"result": "Hello JSON-RPC", "error": null, "id": 1}

This example shows parts of a communication from an example chat application. The chat service sends notifications for each chat message the client peer should receive. The client peer sends requests to post messages to the chat and expects a positive reply to know the message has been posted.

...--> {
"method": "postMessage", "params": ["Hello all!"], "id": 99}<-- {
"result": 1, "error": null, "id": 99}<-- {
"method": "handleMessage", "params": ["user1", "we were just talking"], "id": null}<-- {
"method": "handleMessage", "params": ["user3", "sorry, gotta go now, ttyl"], "id": null}--> {
"method": "postMessage", "params": ["I have a question:"], "id": 101}<-- {
"method": "userLeft", "params": ["user3"], "id": null}<-- {
"result": 1, "error": null, "id": 101}...

Because params field is an array of objects, the following format is also ok:

{
"method": "methodnamehere", "params": [ {
"firstparam": "this contains information of the firstparam.", "secondparam": 1121211234, "thirdparam": "this contains information of the thirdparam." }, {
"fourthparam": "this is already a different object.", "secondparam": "there can be same name fields in different objects.", "thirdparam": "this contains information of the thirdparam." } ], "id": 1234}

Version 1.1 (Working Draft)

The format of the contents of a request might be something like that shown below:

{
"version": "1.1", "method": "confirmFruitPurchase", "id": "194521489", "params": [ ["apple", "orange", "mangoes"], 1.123 ]}

The format of a response might be something like this:

{
"version": "1.1", "result": "done", "error": null, "id": "194521489"}

Version 2.0

Procedure call with positional parameters:

--> {
"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}<-- {
"jsonrpc": "2.0", "result": 19, "id": 1}
--> {
"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}<-- {
"jsonrpc": "2.0", "result": -19, "id": 2}

Procedure call with named parameters:

--> {
"jsonrpc": "2.0", "method": "subtract", "params": {
"subtrahend": 23, "minuend": 42}, "id": 3}<-- {
"jsonrpc": "2.0", "result": 19, "id": 3}
--> {
"jsonrpc": "2.0", "method": "subtract", "params": {
"minuend": 42, "subtrahend": 23}, "id": 4}<-- {
"jsonrpc": "2.0", "result": 19, "id": 4}

Notification:

--> {
"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}
--> {
"jsonrpc": "2.0", "method": "foobar"}

Procedure call of non-existent procedure:

--> {
"jsonrpc": "2.0", "method": "foobar", "id": 10}<-- {
"jsonrpc": "2.0", "error": {
"code": -32601, "message": "Procedure not found."}, "id": 10}

Procedure call with invalid JSON:

--> {
"jsonrpc": "2.0", "method": "foobar", "params": "bar", "baz"]<-- {
"jsonrpc": "2.0", "error": {
"code": -32700, "message": "Parse error"}, "id": null}

Procedure call with invalid JSON-RPC:

--> [1,2,3]<-- [  {
"jsonrpc": "2.0", "error": {
"code": -32600, "message": "Invalid Request"}, "id": null}, {
"jsonrpc": "2.0", "error": {
"code": -32600, "message": "Invalid Request"}, "id": null}, {
"jsonrpc": "2.0", "error": {
"code": -32600, "message": "Invalid Request"}, "id": null}]
--> {
"jsonrpc": "2.0", "method": 1, "params": "bar"}<-- {
"jsonrpc": "2.0", "error": {
"code": -32600, "message": "Invalid Request"}, "id": null}

转载地址:http://teevb.baihongyu.com/

你可能感兴趣的文章
IBM TSM磁带管理操作小记一则
查看>>
ORA-00258: NOARCHIVELOG 模式下的人工存档必须标识日志
查看>>
Java调用bat文件
查看>>
此责任无可用函数
查看>>
java获取数字和汉字
查看>>
excel Option Explicit webadi
查看>>
ICX错误
查看>>
windows Xp NTLDR is missing
查看>>
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
查看>>
Centos 6.x 安装配置MySQL
查看>>
-source 1.5 中不支持 diamond 运算 请使用 -source 7 或更高版本以启用
查看>>
jar包读取资源文件报错:找不到资源文件(No such file or directory)
查看>>
超简单:Linux安装rar/unrar工具与解压到目录示例
查看>>
Eclipse创建Maven Java8 Web项目,并直接部署Tomcat
查看>>
RedHad 7.x服务器操作记录
查看>>
BindException: Cannot assign requested address (Bind failed)解决办法
查看>>
Centos7:Docker安装Gitlab
查看>>
Kafka日志配置
查看>>
logstash 6.x 收集syslog日志
查看>>
Apche Kylin启动报错:UnknownHostException: node1:2181: invalid IPv6 address
查看>>