JSON RPC
2018年12月28日 04:26
编程技术
基本请求结构
{
    "jsonrpc": "2.0", 
    "method": "subtract", 
    "params": [42, 23], 
    "id": 1
}
jsonrpc 为固定参数
method 需要调用的方法
params 方法值,非必须
id 可以是数值或字符串,与响应相对应
基本响应结构
{
    "jsonrpc": "2.0",
    "result": 19, 
    "id": 1
}
通知类型响应为空
批量调用请求结构
[
    {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
    {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
    {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
    {"foo": "boo"},
    {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
    {"jsonrpc": "2.0", "method": "get_data", "id": "9"} 
]
响应结构
[
    {"jsonrpc": "2.0", "result": 7, "id": "1"},
    {"jsonrpc": "2.0", "result": 19, "id": "2"},
    {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null},
    {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"},
    {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
]
根据 id 确定响应的值
响应错误
| code | message | meaning | 
|---|---|---|
| -32700 | Parse error | Invalid JSON was received by the server.An error occurred on the server while parsing the JSON text. | 
| -32600 | Invalid Request | The JSON sent is not a valid Request object. | 
| -32601 | Method not found | The method does not exist / is not available. | 
| -32602 | Invalid params | Invalid method parameter(s). | 
| -32603 | Internal error | Internal JSON-RPC error. | 
| -32000 to -32099 | Server error | Reserved for implementation-defined server-errors. | 
JSON-RPC 与 RESTFUL 比较
json rpc 适用与服务器内部通信,例如分布式架构
restful 适用于对外通信,例如与浏览器,APP 等
参考
1....
剩余内容已隐藏
查看完整文章以阅读更多