问题描述
我正在使用并被卡住了。我举了一个例子来说明我的问题:
I am using jsonrpc4j and got stuck. I made a little example to show my problem:
抽象类:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes(value = { @JsonSubTypes.Type(value = Walnut.class) })
public abstract class Nut {
}
具体子类:
public class Walnut extends Nut {
}
服务接口:
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(),一切都按预期工作,日志打印:
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"}}
如果我打电话给 remoteService.setNut(new 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)
缺少参数的类型信息,因为代理将所有参数包装到一个对象数组中(参见我的l请以了解在这种情况下缺少信息类型的原因。)
The type information of the parameter is missing because the proxy wraps all parameters into one object array (see my last question to understand why the info type is missing in this case).
如何实现所需的序列化?我尝试启用默认输入并通过混合注释(使用@JsonTypeInfo) Object.class ,但都失败了(以下例外)。
How can i achieve the desired serialization? I tried to enable default typing and to annotate (with @JsonTypeInfo) the Object.class via mix-in, but both failed (exceptions below).
启用默认输入[ remoteService.getNut(),服务器端错误]:
With enabled default typing [remoteService.getNut(), error on server side]:
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()),客户端错误]:
With enabled default typing [remoteService.setNut(new Walnut()), error on client side]:
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(),服务器端出错]:
With mix-in [remoteService.getNut(), error on server side]:
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()),客户端出错]:
With mix-in [remoteService.setNut(new Walnut()), error on client side]:
Exception in thread "main" java.lang.ClassCastException:
[Ljava.lang.Object; cannot be cast to
com.fasterxml.jackson.databind.JsonNode
任何想法?
推荐答案
我通过修补库解决了我的问题。现在它逐个序列化所有参数,然后将它们连接起来。可以在。
I solved my issue by patching the library. Now it serializes all parameters one by one and concatenate them afterwards. Bug report and patch can be found at http://code.google.com/p/jsonrpc4j/issues/detail?id=49.
这篇关于jsonrpc4j:如何向参数添加类型信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!