Package org.starcoin.jsonrpc
Class JSONRPC2Response
- java.lang.Object
-
- org.starcoin.jsonrpc.JSONRPC2Response
-
public class JSONRPC2Response extends Object
Represents a JSON-RPC 2.0 response.A response is returned to the caller after a JSON-RPC 2.0 request has been processed (notifications, however, don't produce a response). The response can take two different forms depending on the outcome:
- The request was successful. The corresponding response returns
a JSON object with the following information:
resultThe result, which can be of any JSON type - a number, a boolean value, a string, an array, an object or null.idThe request identifier which is echoed back back to the caller.jsonrpcA string indicating the JSON-RPC protocol version set to "2.0".
- The request failed. The returned JSON object contains:
errorAn object with:codeAn integer indicating the error type.messageA brief error messsage.dataOptional error data.
idThe request identifier. If it couldn't be determined, e.g. due to a request parse error, the ID is set tonull.jsonrpcA string indicating the JSON-RPC protocol version set to "2.0".
Here is an example JSON-RPC 2.0 response string where the request has succeeded:
{ "result" : true, "id" : "req-002", "jsonrpc" : "2.0" }And here is an example JSON-RPC 2.0 response string indicating a failure:
{ "error" : { "code" : -32601, "message" : "Method not found" }, "id" : "req-003", "jsonrpc" : "2.0" }- Author:
- Vladimir Dzhuvinov
- The request was successful. The corresponding response returns
a JSON object with the following information:
-
-
Constructor Summary
Constructors Constructor Description JSONRPC2Response()JSONRPC2Response(Object id)Creates a new JSON-RPC 2.0 response to a successful request which result isnull.JSONRPC2Response(Object result, Object id)Creates a new JSON-RPC 2.0 response to a successful request.JSONRPC2Response(JSONRPC2Error error, Object id)Creates a new JSON-RPC 2.0 response to a failed request.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JSONRPC2ErrorgetError()Gets the error object indicating the cause of the request failure.ObjectgetID()Gets the request identifier that is echoed back to the caller.StringgetJsonRPCVersion()ObjectgetResult()Gets the result of the request.booleanindicatesSuccess()A convinience method to check if the response indicates success or failure of the request.voidsetError(JSONRPC2Error error)Indicates a failed JSON-RPC 2.0 request and sets the error details.voidsetID(Object id)Sets the request identifier echoed back to the caller.voidsetJsonRPCVersion(String jsonRPCVersion)voidsetResult(Object result)Indicates a successful JSON-RPC 2.0 request and sets the result.StringtoString()
-
-
-
Constructor Detail
-
JSONRPC2Response
public JSONRPC2Response()
-
JSONRPC2Response
public JSONRPC2Response(Object result, Object id)
Creates a new JSON-RPC 2.0 response to a successful request.- Parameters:
result- The result. The value can map to any JSON type. May benull.id- The request identifier echoed back to the caller. May benullthough not recommended.
-
JSONRPC2Response
public JSONRPC2Response(Object id)
Creates a new JSON-RPC 2.0 response to a successful request which result isnull.- Parameters:
id- The request identifier echoed back to the caller. May benullthough not recommended.
-
JSONRPC2Response
public JSONRPC2Response(JSONRPC2Error error, Object id)
Creates a new JSON-RPC 2.0 response to a failed request.- Parameters:
error- A JSON-RPC 2.0 error instance indicating the cause of the failure. Must not benull.id- The request identifier echoed back to the caller. Pass anullif the request identifier couldn't be determined (e.g. due to a parse error).
-
-
Method Detail
-
getResult
public Object getResult()
Gets the result of the request. The returned value has meaning only if the request was successful. Use thegetErrormethod to check this.- Returns:
- The result.
-
setResult
public void setResult(Object result)
Indicates a successful JSON-RPC 2.0 request and sets the result. Note that if the response was previously indicating failure this will turn it into a response indicating success. Any previously set error data will be invalidated.- Parameters:
result- The result. The value can map to any JSON type. May benull.
-
getError
public JSONRPC2Error getError()
Gets the error object indicating the cause of the request failure. If anullis returned, the request succeeded and there was no error.- Returns:
- A JSON-RPC 2.0 error object,
nullif the response indicates success.
-
setError
public void setError(JSONRPC2Error error)
Indicates a failed JSON-RPC 2.0 request and sets the error details. Note that if the response was previously indicating success this will turn it into a response indicating failure. Any previously set result data will be invalidated.- Parameters:
error- A JSON-RPC 2.0 error instance indicating the cause of the failure. Must not benull.
-
indicatesSuccess
public boolean indicatesSuccess()
A convinience method to check if the response indicates success or failure of the request. Alternatively, you can use the#getErrormethod for this purpose.- Returns:
trueif the request succeeded,falseif there was an error.
-
getID
public Object getID()
Gets the request identifier that is echoed back to the caller.- Returns:
- The request identifier. If there was an error during the
the request retrieval (e.g. parse error) and the identifier
couldn't be determined, the value will be
null.
-
setID
public void setID(Object id)
Sets the request identifier echoed back to the caller.- Parameters:
id- The value must map to a JSON scalar. Pass anullif the request identifier couldn't be determined (e.g. due to a parse error).
-
getJsonRPCVersion
public String getJsonRPCVersion()
-
setJsonRPCVersion
public void setJsonRPCVersion(String jsonRPCVersion)
-
-