Skip to main content

Exception Handling

JSR 303

  • Introduced Java Bean Validation
  • Part of JEE 6 and above, suported by Spring since version 3
  • Set of annotations used to validate Java Bean properties

JSR 380 - Bean Validation 2.0

  • Added to Spring Framework 5.0
  • Available in Spring Boot 2.0.0+
  • Uses Hibnernate Validatior 6.0+
  • Primary goal of Bean Validation 2.0 is Java 8 language features
  • Added 11 new built in validation annotations

Built in Contstraint Definitions

  • @Null - Checks value is null
  • @NotNull - Checks value is not null
  • @AssertTrue - Value is true
  • @AssertFalse - What do you think?
  • @Min - Number is equal to or higher
  • @Max - Number is equal to or less
  • @DecimalMin - Value is larger
  • @DecimalMax - Value is less than
  • @Negative - Value is less than zero. Zero invalid
  • @NegativeOrZero - Value is less than or zero.
  • @Positive
  • @PositiveOrZero
  • @Size - Checks if string or collection is between a min and max
  • @Digits - checks for integer digits and fraction digits
  • @Past - Checks if a date is in the past
  • @PastOrPresent
  • @Future
  • @FutureOrPresent
  • @Pattern - Checks against RegEx pattern
  • @NotEmpty - Checks if value is null or empty (whitespace or empty collection)
  • @NonBlank - Checks string is not null or not whitespace characters
  • @Email - Checks if string value is an email address

Hibernate Validator Constraints

  • @ScriptAssert - Class level annotation, checks class against script
  • @CreditCardNumber - Verifies value is a credit card number
  • @Currency - Valid currency ammount
  • @DurationMax - Duration less than given value
  • @DurationMin - Duration more than given value
  • @EAN - Valid EAN barcode
  • @ISBN - Valid ISBN value
  • @Length - String length between given min and max
  • @CodePointLength - Validates that code point length of the annotated character sequence is between min and max
  • @LuhnCheck - Luhn check sum
  • @Mod10Check - Mod 10 check sum
  • @Mod11Check - Mod 11 check sum
  • @Range - Checks if a number is between min and max (exlusive)
  • @Safehtml - Checks for safe HTML
  • @UniqueElements - Checks if collection has unqiue elements
  • @Url - Checks for valid URL