java - jsonrpc4j : How to add type info to parameters?

标签 java json serialization jackson deserialization

我正在使用 jsonrpc4j卡住了。我举了一个小例子来说明我的问题:

抽象类:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes(value = { @JsonSubTypes.Type(value = Walnut.class) })
public abstract class Nut {

}

具体子类:

public class Walnut extends Nut {

}

服务接口(interface):

public interface ServiceInterface {
    public Nut getNut();
    public void setNut(Nut nut);
}

服务本身:

public class Service implements ServiceInterface {
    public Nut getNut() { return new Walnut(); }
    public void setNut(Nut nut) {}
}

服务器:

JsonRpcServer rpcServer = new JsonRpcServer(new ObjectMapper(), new Service());
StreamServer streamServer = new StreamServer(rpcServer, 50, 1420,
        50, InetAddress.getByName("127.0.0.1"));
streamServer.start();

客户:

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));

如果我调用 remoteService.getNut() 一切都按预期工作,日志打印:

JSON-PRC Request: {"id":"6064348714687420633","jsonrpc":"2.0","method":"getNut"}
JSON-PRC Response: {"jsonrpc":"2.0","id":"6064348714687420633",
        "result":{"@type":"Walnut"}}

如果我调用remoteService.setNut(new Walnut()),服务器抛出异常,日志打印:

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)

缺少参数的类型信息,因为代理将所有参数包装到一个对象数组中(请参阅我的最后一个 question 以了解为什么在这种情况下缺少信息类型)。

如何实现所需的序列化?我尝试启用默认类型并通过混合注释(使用@JsonTypeInfo)Object.class,但都失败了(以下异常(exception))。

启用默认输入 [remoteService.getNut(),服务器端出错]:

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

启用默认输入 [remoteService.setNut(new Walnut()),客户​​端出错]:

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)

使用混入 [remoteService.getNut(),服务器端出错]:

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]

使用混入 [remoteService.setNut(new Walnut()),客户​​端出错]:

Exception in thread "main" java.lang.ClassCastException:
        [Ljava.lang.Object; cannot be cast to
        com.fasterxml.jackson.databind.JsonNode

有什么想法吗?

最佳答案

我通过修补库解决了我的问题。现在它将所有参数一一序列化,然后将它们连接起来。错误报告和补丁可以在 http://code.google.com/p/jsonrpc4j/issues/detail?id=49 找到.

关于java - jsonrpc4j : How to add type info to parameters?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14631502/

相关文章:

java - 仅 RecyclerView 的第一个 CardView 被着色

java - Hibernate:仅获取关联实体的列表

Java ASM 多次访问一个方法

java - 编写代码打印字符串中任意空格的位置

java - Android NetworkOnMainThreadException 异常

json - 如何创建具有不同类型值的 Json 对象?

java - 具有集合成员的可序列化 JPA 实体

xml - Jackson:XML to Map with List deserialization

json - Grails - 如何注销已注册的对象编码器

arrays - 在 node.js 和 socket.io 中发送数组时出现错误