1

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.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
mba12
  • 2,702
  • 6
  • 37
  • 56

1 Answers1

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()).

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146