13.12.11

Bean Validation Made Simple With JSR 303 | Javalobby

JSR 303

This JSR will define a meta-data model and API for JavaBeanTM validation based on annotations, with overrides and extended meta-data through the use of XML validation descriptors. 

  • JSR 303 provides an easy way of ensuring that the properties of your JavaBean(s) have the right values in them.
  • Pre-JSR 303
    • You probably would have needed a bunch of if-else statements to achieve the above requirements. 
  • The next step is to download an implementation of JSR 303.
    • Hibernate Validator 4.2.0 Final
      • hibernate-validator-4.2.0.Final.jar
      • hibernate-validator-annotation-processor-4.2.0.Final.jar
      • slf4j-api-1.6.1.jar
      • validation-api-1.0.0.GA.jar
  • Simply annotate the 'Member' JavaBean we created earlier to indicate which properties need be validated. 
    • @NotNull
    • @NotBlank
    • @Pattern
    • @Past
    • @Min
    • @Max
    • @Email
  • Unit Test
    • ValidatorFactory
    • Validator
    • violation

No comments: