@Null
is a very important annotation. It is not useless. Let me show a common usecase. Say, there is DAO (entity) object with Id auto-generated.
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
requires just integer Id , only if we want to delete an object with known Id.
There are alternative complicated cases, which usually are not handled but makes sense
@GetMapping
can be used for any provided field but if any of the field other than Id is provided then Id must be blank. If Id is provided then all others must be blank.
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.
Another annotation @DeleteMapping
is used to delete or remove. If it is computed to blank, it can be implented with @Null
constraint.
Usual CRUD operations are too simple but not a suitable usecase for practical expectations.
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.
ID
field that should benull
before saving into DB. Or you have object that should be saved into your system but some field, for example some paymentID should be retrieved and updated by 3rd party service, in that case when you are going to save this object you validate this field @Null, and then some other thread (service) will read this object and update that field additionally. So everything depends on your use cases. – Eyeful