@Null
是一个非常重要的说明。 这并非毫无用处。 请允许我展示一个共同使用案例。 Say,有Id汽车产生的DAO(准号)物体。
If you use separate classes for DTO and DAO, @GetMapping
returns list of DTOs without issues. On the other hand, @PostMapping
for adding a new element requires that input DTO must not contain Id field and even if present it must be null or undefined.
Such an input when converted to and from DTO object, expects Id must be blank.
For this, @Null
is the only choice
@PutMapping
expects id must not be blank so @NotNull
is required for id field when we expect update happens to a single object.
@DeleteMapping
, 只有我们希望删除一个已知的Id的物体时,才要求进行简单的分类。
还有一些其他复杂案件,通常不处理,但具有意义。
@GetMapping
可用于任何已提供领域,但若提供Id以外的任何领域,Id必须空白。 如果提供Id,那么所有其他人都必须是空白。
There is also a complicated @PutMapping
requirement,
where in partial information is provided for update and expected remaining fields to be older values. Here non-null fields are updated.
删除或删除另一个注释:@DeleteMapping
。 如果将其计算成空白,则可以采用@Null
限制。
通常的CRUD业务过于简单,但不适合实际预期。
All these mix of requirements can be listed into groups.
And constraints can be provided with groups attribute with separate Marked interface @Validated
can be applied as per requirement.