问题
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