Spring JPA @NotNull 和@JsonIgnore

Spring JPA @NotNull and @JsonIgnore

如果街道为空,可以做什么?我会在 HTTP POST 中得到一个例外。 NotNull 正在检查 属性 是否为空...

@NotNull
@Column(name = "street")
@JsonIgnore
private String street;

在这种情况下 JsonIgnore 不起作用。为什么?我想,如果我使用 JsonIgnore not null 将不会被检查....

我可以用 @JsonIgnore 或其他注释重载 @NotNull 吗?

如果您想要 "ignore" 空值,您可以使用:

@JsonInclude(Include.NON_NULL)
class Foo
{
  String bar;
}

来自: How to tell Jackson to ignore a field during serialization if its value is null?

为什么 JsonIgnore 和 NotNull 不起作用?

基本上JSonIgnore注解和Not Null没有关系,因为第一个是Jackson,另一个是JPA。

我相信这些话题可以帮助到你:

Serializing JPA entities to JSON using Jackson

Confusion: @NotNull vs @Column(nullable = false)

http://www.davismol.net/2015/03/10/jackson-json-difference-between-jsonignore-and-jsonignoreproperties-annotations/