[英]How to implement with AutoBean a list of different base types like String, Integer, etc.?
I want to create a request for JSON-RPC with three parameters - String, Integer and my own object. 我想用三个参数创建一个JSON-RPC请求 - String,Integer和我自己的对象。 Request should look like this: 请求应如下所示:
{"method":"MyMethod", "params":["text", 123, {"name": "any text", "num": 15}], "id":1}
Ideally, I would like to create an AutoBean like this (but it does not work): 理想情况下,我想像这样创建一个AutoBean(但它不起作用):
interface JsonRpcRequest {
String getJsonrpc();
void setJsonrpc(String value);
String getMethod();
void setMethod(String value);
List<Object> getParams(); // ERROR: Type Object may not be used
void setParams(List<Object> params); // ERROR: Type Object may not be used
}
interface JsonRpcRequestFactory extends AutoBeanFactory {
AutoBean<JsonRpcRequest> jsonRpcRequest();
}
The problem is that the AutoBean framework does not allows the use of List<Object>
inside interface. 问题是AutoBean框架不允许在接口内部使用List<Object>
。
Is there another way to create a list/array of elements of different based and non-based types? 是否有另一种方法来创建不同基础和非基础类型的元素的列表/数组?
No, you simply can't. 不,你根本做不到。 AutoBean requires everything to be statically typed : no polymorphism, and no mixed-typed lists of maps. AutoBean要求静态输入所有内容 :没有多态,也没有混合类型的地图列表。
You might be interested by RequestFactory's built-in support for JSON-RPC though. 您可能对RequestFactory对JSON-RPC的内置支持感兴趣。
Why do your params all need to be passed back in a list? 为什么你的params都需要传回列表? Surely you're not going to do the same thing with a String
, an Integer
, and another Object
! 当然你不会用String
, Integer
和另一个Object
做同样的事情! Just send them all back separately. 只需将它们全部分开寄回。
Further, you're not sending a custom Object
over the JSON, you're sending the objid
of that object... so just send the Integer id
and let the server handle it. 此外,您不是通过JSON发送自定义Object
,而是发送该对象的objid
......所以只需发送Integer id
并让服务器处理它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.