How to get Jackson to not output a field to XML when serializing

泪湿孤枕 提交于 2020-01-30 08:12:07

问题


Similar to this question I want to ignore a field in my java POJO. However, I want to always ignore the field whether or not it is null.

In my case I need to populate the field value for logic but later when I am outputting my xml I do not want this field included.

@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;
}

The output should look like this:

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

Have experimented with @JsonIgnore but when used the field does not get populated.


回答1:


You should use JsonIgnoreProperties:

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

From documentation for allowSetters:

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



来源:https://stackoverflow.com/questions/58205564/how-to-get-jackson-to-not-output-a-field-to-xml-when-serializing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!