如何在Jackson负载中排除布尔类型的值

huangapple 未分类评论49阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年4月9日 14:13:47
  • 转载请务必保留本文链接:https://java.coder-hub.com/61115006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定