返回带有1个对象和2个其他值的自定义json

时间:2017-01-24 12:33:49

标签: json spring jackson

我和杰克逊一起使用Spring,我需要返回类似

的内容
{
"user": {
    "username":"myusername",
    "mail":"mymail"
    },
"custom": {
    "hasVoted":"true",
    "hasCommented":"false"
    }
}

直到今天,我只发回一个转换为jackson的User对象,但现在我需要添加一个自定义参数。我怎样才能做到这一点 ?感谢。

1 个答案:

答案 0 :(得分:0)

Map<String, Object> test = new HashMap<String, Object>();
    Map<String, Boolean> custom = new HashMap<>();
    custom.put("hasVoted", true);
    custom.put("hasCommented", false);
    test.put("user", User);
    test.put("custom", custom);
    return test;