java – jsonrpc4j:如何向参数添加类型信息?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – jsonrpc4j:如何向参数添加类型信息?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2976字,纯文字阅读大概需要5分钟。
内容图文
我正在使用jsonrpc4j而且卡住了.我举了一个例子来说明我的问题:
抽象类:
@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(),一切都按预期工作,日志打印:
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,但都失败了(下面的例外).
启用默认输入[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)
使用mix-in [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]
使用mix-in [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:如何向参数添加类型信息?全部内容,希望文章能够帮你解决java – jsonrpc4j:如何向参数添加类型信息?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。