java - Genson:使用 RuntimePropertyFilter 时处理子属性

标签 java json genson

对于我的 Genson 配置,我使用 UrlQueryParamFilter 。它有效,但不像我预期的那样。
我的实体:

public class Root {
    private String firstRootProp;
    private String secondRootProp;
    private List<Child> childs;

    //getters & setters
}

public class Child {
    private String firstChildProp;
    private String secondChildProp;

    //getters & setters
}

“rootEntity”端点绑定(bind)到获取一些Root以我的休息服务为例。 当我得到http://<host>/myservice/rootEntity?filter=childs时我预计将获得所有 child 以及所有 child 的属性(property)。但实际上我只得到了 child 的结构:

{
    "childs": [
        {},
        {}
    ]
}

我想要得到什么:

{
    "childs": [
        {
            "firstChildProp": "Some value for first property",
            "secondChildProp": "And some value for second property"
        },
        {
            "firstChildProp": "Some value for first property",
            "secondChildProp": "And some value for second property"
        }
    ]
}

如何修复它?
谢谢。

最佳答案

UrlQueryParamFilter 希望您提供要包含的所有属性的名称(如果配置为排除属性,则要排除)。简而言之,做 http://<host>/myservice/rootEntity?filter=childs,firstChildProp,secondChildProp应该可以。

我想提供一种配置包含所有子属性的方法可能是有意义的,我打开了这个问题https://github.com/owlike/genson/issues/105 .

关于java - Genson:使用 RuntimePropertyFilter 时处理子属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39532752/

相关文章:

java - 连接到代理防火墙后面的 DBpedia 端点

java - Genson 1.4 (JSON) 不处理继承的 Pojo 的

java - Memcache 可序列化的 JSONObject?

java - 如何在 Jersey 客户端中默认使用 Genson 提供程序?

java - 无法使用 Jersey 解析 RESTful 应用程序中的日期

java - 哪些 MVC 框架可用于 Java 桌面/winform 应用程序?

java - 打印字符串而不是斐波那契数

java - 带有 Tomcat 的 JSR-356 WebSockets - 如何限制单个 IP 地址内的连接?

json - 如何使用 Azure 函数 Node.js 读取 Json 文件

mysql - 更新触发器不适用于 json 数据类型