Note that there are some explanatory texts on larger screens.

plurals
  1. POjsonrpc4j: How to add type info to parameters?
    primarykey
    data
    text
    <p>I am using <a href="http://code.google.com/p/jsonrpc4j/" rel="nofollow noreferrer">jsonrpc4j</a> and got stuck. I made a little example to show my problem:</p> <p>Abstract class:</p> <pre><code>@JsonTypeInfo(use = JsonTypeInfo.Id.NAME) @JsonSubTypes(value = { @JsonSubTypes.Type(value = Walnut.class) }) public abstract class Nut { } </code></pre> <p>Concrete subclass:</p> <pre><code>public class Walnut extends Nut { } </code></pre> <p>Interface for the service:</p> <pre><code>public interface ServiceInterface { public Nut getNut(); public void setNut(Nut nut); } </code></pre> <p>The service itself:</p> <pre><code>public class Service implements ServiceInterface { public Nut getNut() { return new Walnut(); } public void setNut(Nut nut) {} } </code></pre> <p>Server:</p> <pre><code>JsonRpcServer rpcServer = new JsonRpcServer(new ObjectMapper(), new Service()); StreamServer streamServer = new StreamServer(rpcServer, 50, 1420, 50, InetAddress.getByName("127.0.0.1")); streamServer.start(); </code></pre> <p>Client:</p> <pre><code>JsonRpcClient jsonRpcClient = new JsonRpcClient(new ObjectMapper()); ServiceInterface remoteService = ProxyUtil.createClientProxy( ServiceInterface.class.getClassLoader(), ServiceInterface.class, jsonRpcClient, new Socket(InetAddress.getByName("127.0.0.1"), 1420)); </code></pre> <p>If i call <em>remoteService.getNut()</em> everything works as expected, the log prints:</p> <pre><code>JSON-PRC Request: {"id":"6064348714687420633","jsonrpc":"2.0","method":"getNut"} JSON-PRC Response: {"jsonrpc":"2.0","id":"6064348714687420633", "result":{"@type":"Walnut"}} </code></pre> <p>If i call <em>remoteService.setNut(new Walnut())</em> the server throws an exception, the log prints:</p> <pre><code>JSON-PRC Request {"id":"9194853851254039397","jsonrpc":"2.0", "method":"setNut","params":[{}]} Error in JSON-RPC Service com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property '@type' that is to contain type id (for class Nut) </code></pre> <p>The type information of the parameter is missing because the proxy wraps all parameters into one object array (see my last <a href="https://stackoverflow.com/q/14582343/2021412">question</a> to understand why the info type is missing in this case).</p> <p>How can i achieve the desired serialization? I tried to enable default typing and to annotate (with @JsonTypeInfo) the <em>Object.class</em> via mix-in, but both failed (exceptions below).</p> <p>With enabled default typing [<em>remoteService.getNut()</em>, error on server side]:</p> <pre><code>Exception while handling request com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class com.fasterxml.jackson.databind.JsonNode </code></pre> <p>With enabled default typing [<em>remoteService.setNut(new Walnut())</em>, error on client side]:</p> <pre><code>Exception in thread "main" java.lang.IllegalArgumentException: Unexpected token (START_ARRAY), expected VALUE_STRING: need JSON String that contains type id (for subtype of com.fasterxml.jackson.databind.JsonNode) </code></pre> <p>With mix-in [<em>remoteService.getNut()</em>, error on server side]:</p> <pre><code>Exception while handling request java.lang.IllegalArgumentException: Could not resolve type id 'DefaultErrorResolver$ErrorData' into a subtype of [simple type, class com.fasterxml.jackson.databind.JsonNode] </code></pre> <p>With mix-in [<em>remoteService.setNut(new Walnut())</em>, error on client side]:</p> <pre><code>Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.fasterxml.jackson.databind.JsonNode </code></pre> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload