I am using jsonrpc4j and am stuck. I made a small example to show my problem:
Abstract class:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME) @JsonSubTypes(value = { @JsonSubTypes.Type(value = Walnut.class) }) public abstract class Nut { }
Specific subclass:
public class Walnut extends Nut { }
Interface for service:
public interface ServiceInterface { public Nut getNut(); public void setNut(Nut nut); }
The service itself:
public class Service implements ServiceInterface { public Nut getNut() { return new Walnut(); } public void setNut(Nut nut) {} }
Server:
JsonRpcServer rpcServer = new JsonRpcServer(new ObjectMapper(), new Service()); StreamServer streamServer = new StreamServer(rpcServer, 50, 1420, 50, InetAddress.getByName("127.0.0.1")); streamServer.start();
Customer:
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));
If I call remoteService.getNut (), everything works as expected, the log prints:
JSON-PRC Request: {"id":"6064348714687420633","jsonrpc":"2.0","method":"getNut"} JSON-PRC Response: {"jsonrpc":"2.0","id":"6064348714687420633", "result":{"@type":"Walnut"}}
If I call remoteService.setNut (new Walnut ()), the server throws an exception, the log prints:
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)
There is no information about the type of the parameter, since the proxy server wraps all the parameters in one array of objects (see my last question ) to understand why this is the case).
How can I achieve the desired serialization? I tried to enable the default input and annotate (using @JsonTypeInfo) Object.class via mix-in, but both of them failed (exceptions below).
With the default setting enabled [remoteService.getNut (), server-side error]:
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
With the default setting enabled [remoteService.setNut (new Walnut () file), client-side error]:
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)
With mix-in [remoteService.getNut (), server-side error]:
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]
With mix-in [remoteService.setNut (new Walnut () file), client-side error]:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.fasterxml.jackson.databind.JsonNode
Any ideas?