【问题标题】:How do I configure a Jackson ObjectMapper to show the root value?如何配置 Jackson ObjectMapper 以显示根值?
【发布时间】:2015-01-21 12:59:48
【问题描述】:

在我的 Spring 项目中,我添加了 jackson 1,现在它是 2,然后我看到了这种差异。以前的响应是 'loginResponse' 现在在 Json 中没有对象名称。
loginResponse 的旧对象类如下:

public class LoginResponse {

    private String code;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

}

以下是我的 loginResponse 新对象类​​,带有注释和 SerializationFeature.WRAP_ROOT_VALUE,false:

@JsonRootName(value = "loginResponse") 
public class LoginResponse {

private String code;

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

ObjectMapper aa= new ObjectMapper().configure(SerializationFeature.WRAP_ROOT_VALUE, false);

}

我想要这个输出:

{
   "loginResponse":
   {
       "code": 0
   }
}

但它给了我以下回应:

 {
       "page": 0
}

请任何人知道这一点。如何解决?请。

【问题讨论】:

  • 您的示例有问题:codepage 不匹配,listResponseloginResponse 也不匹配。

标签: java json spring jackson


【解决方案1】:

我相信 SerializationFeature.WRAP_ROOT_VALUE 应该设置为 true,而不是像上面 sn-p 中的 false。

【讨论】:

  • 即使结果相同,我也设置为 true。
【解决方案2】:

您将其设置为 false 而不是 true。

使用这个。

ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 2021-10-24
    • 2012-10-17
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多