- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
问题: 是否可以在创建 ObjectMapper
时覆盖 POJO 中定义的 Include.NON_NULL
?
解释:
假设我有一个如下所示的 POJO:
@JsonInclude(Include.NON_NULL)
class POJO {
String name;
String description;
//Constructors, Getters & Setters, etc
}
还有一个测试类如下:
class Main {
public static void main(String args[]) {
POJO p = new POJO();
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.setSerializationInclusion(Include.ALWAYS)
.writerWithDefaultPrettyPrinter()
.writeValueAsString(p);
//jsonString should contain empty name & description fields, but they doesn't
}
}
最佳答案
您可以使用混合,因为它优先于注释。
@JsonInclude(JsonInclude.Include.ALWAYS)
public class MixIn {
}
并将其添加到映射器
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(POJO.class, MixIn.class);
结果会是
{"name":null,"description":null}
关于java - 从 ObjectMapper 中的 POJO 覆盖 @JsonInclude(Include.NON_NULL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45571979/
目前我的应用程序有一个类如下 @JsonInclude(JsonInclude.Include.NON_NULL) public class Employee { Department depar
我在 Response 类上添加了 @JsonInclude(Include.NON_NULL) 注释。 @JsonInclude(Include.NON_NULL) public class Res
我通常使用 objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL) 因为我从不想序列化类的空值。除了现在我有一
我正在使用 Spring Boot 1.5.7-发布 . 我正在尝试设置 全局非空 我的应用程序中的 Jackson 属性。 但它不起作用。 我在 application.properties 和 b
我用 @JsonInclude(JsonInclude.Include.NON_NULL) 注释了 POJO,以在序列化/反序列化时排除所有 null 字段。但现在我需要从这种行为中排除其中一个字段。
在 route!=0 的情况下,我正在尝试检索 {"status":201} 但我得到的是 {"status":201 "distance":0.0} 如何使用 @JsonInclude 或 Jack
当使用以下 JSON 并且“phones”或“emailAddresses”为空时,我将收到 NullPointerException。 JSON: { "item": { "messag
我已经在类/pojo 上放置了注释并配置了映射器,但它仍然序列化 null 值 我正在使用 Hibernate 4.3.7Final 和 Jackson 2.4.4。这些集合是延迟加载 Pojo:删除
我试图了解 @JsonInclude 存在的原因是什么,以便在我的 DTO 中使用它。让我们看一下这个简单的例子: 代码: class DemoApplication { static voi
我是 Obj-C 的新手,但我正在尝试在 Xcode 7 上构建一些以前在 Xcode 6 中运行的代码。具体来说,我遇到了这个问题: [_webView loadData:[_text dataUs
问题: 是否可以在创建 ObjectMapper 时覆盖 POJO 中定义的 Include.NON_NULL? 解释: 假设我有一个如下所示的 POJO: @JsonInclude(Include.
我有一个像这样的模型类: @JsonInclude(JsonInclude.Include.NON_NULL) public class Defect 我需要 JsonInclude.Include.
我有一个使用 Apache CXF 的 JAX-RS API。最近我从 Jettison 切换到 Jackson (2.7.1) 来处理 JSON。我正在使用 JacksonJaxbJsonProvi
我正在尝试通过 application.yml 文件在应用程序级别添加 jackson 注释。但是当我运行应用程序时,jackson 注释被忽略并且响应也具有 null 属性。有人帮我吗?我在我的 a
我是一名优秀的程序员,十分优秀!