java - 如何让杰克逊在序列化时不将字段输出到 XML
问题描述
与这个问题类似,我想忽略我的 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
:
可以启用以允许使用“setter”的属性(即,防止忽略 value() 中列出的属性的 setter)。
推荐阅读
- swift - 使用 firebase 更新值
- c# - 如何将 WPF 值绑定到类中的类
- python - ffmpeg hls 段在 5300 段后停止分段
- vba - Word 2016/2019 在 wdDialogFileNew 上崩溃
- javascript - 在采取任何行动之前如何正确等待退货?
- php - 分块调用 Microsoft Graph 日历时出现 504 网关超时
- asp.net - 如何实现 .NET Core 以支持多个操作系统
- django - 如何在 django 中指向模板
- sql-server - 连接表并在一行中显示第二个表数据
- spark-ar-studio - 视频纹理 Spark AR 在设备上运行时崩溃