简体   繁体   English

如何使用AutoBean实现不同基类型的列表,如String,Integer等?

[英]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 ! 当然你不会用StringInteger和另一个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.

相关问题 如何实现注释以限制成员函数,函数类型等数目,例如FunctionalInterface? - How to implement annotations to limit number of member functions, function types, etc. like FunctionalInterface? 如何使用AutoBean使用params实现方法? - How to implement method with params with AutoBean? 如何反映像java.Math这样的类(我想提取参数的函数类型,如:Math.abs()等) - How to reflect classes like java.Math(I would like to extract parameters' types of functions like: Math.abs(), etc.) 将某些内容与数据类型进行比较(例如char,string等) - Comparing something something to a data type (like char, string, etc.) 在像 string_ 这样的文件中写入 '\t' '\n' 等字符? - writing '\t' '\n' etc. characters in a file like a string_? 如何在simpleadapter中实现OnItemClickListener? 代码删除,更新等, - how to implement OnItemClickListener in simpleadapter? code delete, update etc., 如何在JDK中找到与平台相关的陷阱的完整列表,如时区,编码,行结尾等? - How can I find complete list of platform-dependent traps in JDK like timezone, encoding, line endings etc.? Java解析具有不同对象类型(Gson或Jackson等等)的数组的Json - Java Parse Json with array with different object types (Gson or Jackson or etc.) Java ArrayList,在一行中获取多种类型(int,String等)的用户输入 - Java ArrayList, taking user input of multiple types(int, String etc.) in one line 使用Lambda在Java中合并两个不同类型的排序列表(String和Integer) - Merging two sorted list of different types (String and Integer) in Java using Lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM