与此类似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/