Card 01/ 06

ConceptDifficulty: Intermediate1 min

Constraints as Annotations

Jakarta Bean Validation lets a constraint live on the field it applies to, right next to the type — no separate validation function to write and keep in sync with the class.

java
record RideRequest(
    @NotBlank(message = "Pickup location is required") String pickup,
    @Min(value = 1, message = "Passengers must be at least 1") int passengers
) {}
Common constraint annotations
AnnotationRequires
@NotNullNot null — says nothing about emptiness
@NotBlankNot null, not empty, not only whitespace — for String
@NotEmptyNot null and not empty — for a String or a collection
@Min / @MaxA numeric value within a bound
@SizeA String, collection or array within a length range
@PatternA String matching a regular expression

Every constraint here is just metadata on its own — nothing reads it or acts on it until the next card's annotation is added to the controller method itself.