英文:
How to exclude boolean type value for jackson payload
问题
我有一个非常简单的要求,希望在Jackson序列化期间从负载中排除布尔类型属性。以下是我想要修复的代码片段。我希望无论其值如何,始终将其排除。
@Getter
@Setter
@NoArgsConstructor
@XmlRootElement
public class Order{
@JsonIgnore
private boolean userPresent;
}
有人能帮我解决这个问题吗?
英文:
I have really simple requirement to exclude Boolean type attribute from payload during Jackson serialization. Following is the piece of code that I want to fix that. I want exclude it always irrespective to its value.
@Getter
@Setter
@NoArgsConstructor
@XmlRootElement
public class Order{
@JsonIgnore
private boolean userPresent;
}
Can someone help me on this?
答案1
得分: 0
你应该为你想要忽略的属性明确添加Getter
,并在那里设置@JsonIgnore
:
@Getter
@Setter
@NoArgsConstructor
@XmlRootElement
public class Order{
private boolean userPresent;
@JsonIgnore
public boolean isUserPresent() {
return this.userPresent;
}
}
如果在类中没有其他属性,你应该移除@Getter
注解,因为它现在是多余的。
英文:
You should explicitly add the Getter
for the property you want to ignore and set the @JsonIgnore
there:
@Getter
@Setter
@NoArgsConstructor
@XmlRootElement
public class Order{
private boolean userPresent;
@JsonIgnore
public boolean isUserPresent() {
return this.userPresent;
}
}
If you don't have any other properties in the class, you should remove the @Getter
annotation because it is redundant now.
专注分享java语言的经验与见解,让所有开发者获益!
评论