【发布时间】:2022-02-07 10:18:35
【问题描述】:
我正在尝试使用 MongoDB 开发 Spring Boot,但我的问题是我不希望用户为某些字段输入 DB empty 或 null 值。
我已经尝试了我在网上找到的选项,但它不起作用。
代码:
@Data
@Entity
@Document(collection = "product")
public class Product {
@Id
private String id;
@NotEmpty
private String name;
@NotNull(message = "brand must not be null")
private String brand;
@NotNull(message = "barCode must not be null")
private String barCode;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate importDate;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate expireDate;
@NotNull(message = "price must not be null")
private Double price;
private Double deal;
@NotNull(message = "the quantity must not be null")
private Integer quantity;
}
【问题讨论】:
-
当字段值为空或为空时,可能有一个默认值。它是一个什么样的领域?您在寻找验证 - 在哪里?在数据库服务器,还是应用服务器级别?如果您不希望用户输入空值或空值,也许可以在客户端验证时注意。
-
我知道我可以在视图中放更多的行,但是我不想使用很多 if 语句,它不能像 sql 一样
标签: java spring mongodb spring-boot spring-mvc