展平杰克逊中的嵌套属性 [英] Flattening nested attributes in Jackson

查看:100
本文介绍了展平杰克逊中的嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定义一个平面POJO,它将(平面)属性映射到JSON规范中的嵌套对象。用代码更好地解释

I have the need of defining a flat POJO that maps its (flat) attributes to a nested object in its JSON specification. Better explain with code

{
    "offset": 0,
    "pageSize": 10,
    "filter": {
        "key1":"value1",
        "key2": true,
        ....
    }
}

我的POJO如下所示:

My POJO shall look like the following:

public class Pojo {
    private int offset;
    private int pageSize;

    private String key1;
    private boolean key2;
}

到目前为止,我已尝试注释那些属性 @JsonProperty 及其属性

So far I have tried annotating those key properties with @JsonProperty with its value attribute

@JsonProperty("filter.key1")
private String key1;

但是当我进入MVC控制器时,这些属性虽然在JSON中设置,但在解码时为null POJO。

But when I went into the MVC controller those properties, though set in JSON, were null in the decoded POJO.

我该如何解决这个问题?我做错了什么?

How can I fix this? What did I do wrong?

我绝对不想创建嵌套的子类

I absolutely don't want to create nested subclasses

推荐答案

可能目前是不可能的。

这是因为Jackson目前支持 @JacksonUnwrapped 对于相反的情况,但没有 @JacksonWrapped

This because Jackson currently supports @JacksonUnwrapped for the opposite case, but no @JacksonWrapped

  • Discussion here: http://jackson-users.ning.com/forum/topics/use-annotations-to-skip-levels
  • Feature request: https://github.com/FasterXML/jackson-annotations/issues/42

这篇关于展平杰克逊中的嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆