Spring Framework - Validation

5,284 views
5,104 views

Published on

Validation for Spring based projects with native spring validator and with jsr-303.

Published in: Technology, Education
0 Comments
10 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,284
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
297
Comments
0
Likes
10
Embeds 0
No embeds

No notes for slide

Spring Framework - Validation

  1. 1. Spring Framework - Validation SPRING FRAMEWORK 3.0Dmitry Noskov Validation, JSR-303
  2. 2. Spring Validation Spring Framework - Validation Dmitry Noskov
  3. 3. Spring Validatorpublic interface Validator { /** Can this instances of the supplied clazz */ boolean supports(Class<?> clazz); /** * Validate the supplied target object, which must be * @param target the object that is to be validated * @param errors contextual state about the validation process */ void validate(Object target, Errors errors);} Spring Framework - Validation Dmitry Noskov
  4. 4. Simple Spring validatorpublic class MakeValidator implements Validator { public boolean supports(Class<?> c) {return Make.class.equals(c);} public void validate(Object target, Errors errors) { ValidationUtils.rejectIfEmpty(errors, "name", "er.required"); Make make = (Make)target; if (make.getName().length()<3) { errors.rejectValue("name", "er.minlength"); } else if (make.getName().length()>20) { errors.rejectValue("name", "er.maxlength"); } }} Spring Framework - Validation Dmitry Noskov
  5. 5. Auxiliary classes Errors  reject  rejectValue ValidationUtils  rejectIfEmpty  rejectIfEmptyOrWhitespace  invokeValidator Spring Framework - Validation Dmitry Noskov
  6. 6. Resolving codes will create message codes for an object error  code + "." + object name  code will create message codes for a field  code + "." + object name + "." + field  code + "." + field  code + "." + field type  code Spring Framework - Validation Dmitry Noskov
  7. 7. JSR-303a specification for Bean Validation Spring Framework - Validation Dmitry Noskov
  8. 8. Old validation solution Spring Framework - Validation Dmitry Noskov
  9. 9. DDD with JSR-303 Spring Framework - Validation Dmitry Noskov
  10. 10. Fundamentals Annotation Constraint Message Validator Validator Constraint Violation Spring Framework - Validation Dmitry Noskov
  11. 11. Constraints applicable to class, method, field custom constraints composition object graphs properties:  message  groups  payload Spring Framework - Validation Dmitry Noskov
  12. 12. Standard constraintsAnnotation Type Description@Min(10) Number must be higher or equal@Max(10) Number must be lower or equal@AssertTrue Boolean must be true, null is valid@AssertFalse Boolean must be false, null is valid@NotNull any must not be null@NotEmpty String / Collection’s must be not null or empty@NotBlank String @NotEmpty and whitespaces ignored@Size(min,max) String / Collection’s must be between boundaries@Past Date / Calendar must be in the past@Future Date / Calendar must be in the future@Pattern String must math the regular expression Spring Framework - Validation Dmitry Noskov
  13. 13. Examplepublic class Make { @Size(min = 3, max = 20) private String name; @Size(max = 200) private String description;} Spring Framework - Validation Dmitry Noskov
  14. 14. Validator methodspublic interface Validator { /** Validates all constraints on object. */ validate(T object, Class<?>... groups) /** Validates all constraints placed on the property of object*/ validateProperty(T object, String pName, Class<?>... groups) /** Validates all constraints placed on the property * of the class beanType would the property value */ validateValue(Class<T> type, String pName, Object val,Class<?>…)} Spring Framework - Validation Dmitry Noskov
  15. 15. ConstraintViolation exposes constraint violation context core methods  getMessage  getRootBean  getLeafBean  getPropertyPath  getInvalidValue Spring Framework - Validation Dmitry Noskov
  16. 16. Validating groups separate validating simple interfaces for grouping inheritance by standard java inheritance composition combining by @GroupSequence Spring Framework - Validation Dmitry Noskov
  17. 17. Grouping(1) grouping interface public interface MandatoryFieldCheck { } using public class Car { @Size(min = 3, max = 20, groups = MandatoryFieldCheck.class) private String name; @Size(max = 20) private String color; } Spring Framework - Validation Dmitry Noskov
  18. 18. Grouping(2) grouping sequence @GroupSequence(Default.class, MandatoryFieldCheck.class) public interface CarChecks { } using javax.validation.Validator validator; validator.validate(make, CarChecks.class); Spring Framework - Validation Dmitry Noskov
  19. 19. Composition annotation @NotNull @CapitalLetter @Size(min = 2, max = 14) @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ METHOD, FIELD, ANNOTATION_TYPE }) public @interface CarNameConstraint { } using @CarNameConstraint private String name; Spring Framework - Validation Dmitry Noskov
  20. 20. Custom constraint create annotation@Constraint(validatedBy=CapitalLetterValidator.class)public @interface CapitalLetter { String message() default "{carbase.error.capital}"; implement constraint validatorpublic class CapitalLetterValidator implements ConstraintValidator<CapitalLetter, String> { define a default error messagecarbase.error.capital=The name must begin with a capital letter Spring Framework - Validation Dmitry Noskov
  21. 21. LocalValidatorFactoryBean Spring JSR-303 Validator Validator Spring Adapter Spring Framework - Validation Dmitry Noskov
  22. 22. Configuration define bean <bean id="validator"class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>or <mvc:annotation-driven/> injecting @Autowired private javax.validation.Validator validator;or @Autowired private org.springframework.validation.Validator validator; Spring Framework - Validation Dmitry Noskov
  23. 23. Information JSR-303 reference  http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html/  http://static.springsource.org/spring/docs/3.0.x/spring-framework- reference/html/validation.html samples  http://src.springsource.org/svn/spring-samples/mvc-showcase blog  http://blog.springsource.com/category/web/ forum  http://forum.springsource.org/forumdisplay.php?f=25 Spring Framework - Validation Dmitry Noskov
  24. 24. Questions Spring Framework - Validation Dmitry Noskov
  25. 25. The end http://www.linkedin.com/in/noskovd http://www.slideshare.net/analizator/presentations

×