Card 01/ 06
All 6 cards
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.
record RideRequest(
@NotBlank(message = "Pickup location is required") String pickup,
@Min(value = 1, message = "Passengers must be at least 1") int passengers
) {}| Annotation | Requires |
|---|---|
@NotNull | Not null — says nothing about emptiness |
@NotBlank | Not null, not empty, not only whitespace — for String |
@NotEmpty | Not null and not empty — for a String or a collection |
@Min / @Max | A numeric value within a bound |
@Size | A String, collection or array within a length range |
@Pattern | A 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.