java - 如何让 Jackson 在序列化时不将字段输出到 XML

标签 java xml serialization jackson deserialization

与此类似question我想忽略 java POJO 中的一个字段。但是,我想始终忽略该字段,无论它是否为空。

就我而言,我需要填充逻辑字段值,但稍后当我输出 xml 时,我不希望包含此字段。

@AllArgsConstructor
@NoArgsConstructor
public class MyNotification {
    @Setter @Getter private String id;
    @Setter @Getter private String time;
    @Setter @Getter private String flag;
    @Setter @Getter private String event;
    @Setter @Getter private String type;
}

输出应如下所示:

    <MyNotification>
        <id>112314</id>
        <time>2019-08-20T08:41:45Z</time>
        <flag>new</flag>
        <type>T01</type>
    </MyNotification>

已尝试使用@JsonIgnore,但使用时该字段不会被填充。

最佳答案

您应该使用JsonIgnoreProperties :

@JsonIgnoreProperties(value = "event", allowSetters = true)
class MyNotification

来自 allowSetters 的文档:

Property that can be enabled to allow "setters" to be used (that is, prevent ignoral of setters for properties listed in value()).

关于java - 如何让 Jackson 在序列化时不将字段输出到 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58992537/

相关文章:

xml - 用 R 抓取维基百科来制作列表和数据框

c# - XmlSerializer (c#) 报告反射(reflect)类型时出现错误 (type = List<myclass> )

c# - 是否可以通过序列化将属性应用于对象?

java - 无法连接到我的数据库服务器 : ArrayIndexOutOfBoundsException

java - 扫描仪变量在 try-catch block 之外不起作用

java - Jython 通过关键字参数传递来调用 Java 方法

html - XPath轴中的子代,后代和后代之间的差异

javascript - 如何将 JSON 数据转换为 XML?

python - PyTorch 中的 .pt、.pth 和 .pwf 扩展名有什么区别?

java - 如何检查我的 arraylist 中是否有不在 hashmap 中的元素?