Spring Framework - Validation

analizator 4,974 views 25 slides Aug 11, 2011
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

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


Slide Content

SPRING FRAMEWORK 3.0
Validation, JSR-303 Dmitry Noskov
Spring Framework - Validation

Spring Validation
Dmitry Noskov Spring Framework - Validation

Spring Validator
Dmitry Noskov Spring Framework - Validation
public 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);
}

Simple Spring validator
Dmitry Noskov Spring Framework - Validation
public 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");
}
}
}

Auxiliary classes
Dmitry Noskov Spring Framework - Validation
Errors
reject
rejectValue

ValidationUtils
rejectIfEmpty
rejectIfEmptyOrWhitespace
invokeValidator

Resolving codes
Dmitry Noskov Spring Framework - Validation
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

a specification for Bean Validation
JSR-303
Dmitry Noskov Spring Framework - Validation

Old validation solution
Dmitry Noskov Spring Framework - Validation

DDD with JSR-303
Dmitry Noskov Spring Framework - Validation

Fundamentals
Dmitry Noskov Spring Framework - Validation
Validator
Annotation
Constraint
Validator
Constraint
Violation
Message

Constraints
Dmitry Noskov Spring Framework - Validation
applicable to class, method, field
custom constraints
composition
object graphs
properties:
message
groups
payload

Standard constraints
Dmitry Noskov Spring Framework - Validation
Annotation 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

Example
Dmitry Noskov Spring Framework - Validation


public class Make {

@Size(min = 3, max = 20)
private String name;

@Size(max = 200)
private String description;
}

Validator methods
Dmitry Noskov Spring Framework - Validation

public 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<?>…)
}

ConstraintViolation
Dmitry Noskov Spring Framework - Validation
exposes constraint violation context
core methods
getMessage
getRootBean
getLeafBean
getPropertyPath
getInvalidValue

Validating groups
Dmitry Noskov Spring Framework - Validation
separate validating
simple interfaces for grouping
inheritance by standard java inheritance
composition
combining by @GroupSequence

Grouping(1)
Dmitry Noskov Spring Framework - Validation
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;
}

Grouping(2)
Dmitry Noskov Spring Framework - Validation
grouping sequence
@GroupSequence(Default.class, MandatoryFieldCheck. class)
public interface CarChecks {
}
using
javax.validation.Validator validator;
validator.validate(make, CarChecks.class);

Composition
Dmitry Noskov Spring Framework - Validation
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;

Custom constraint
Dmitry Noskov Spring Framework - Validation
create annotation
@Constraint(validatedBy=CapitalLetterValidator .class)
public @interface CapitalLetter {
String message() default "{carbase.error.capital }";
implement constraint validator
public class CapitalLetterValidator implements
ConstraintValidator<CapitalLetter, String> {
define a default error message
carbase.error.capital =The name must begin with a capital letter

LocalValidatorFactoryBean
Dmitry Noskov Spring Framework - Validation
Spring
Adapter
Spring
Validator
JSR-303
Validator

Configuration
Dmitry Noskov Spring Framework - Validation
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;

Information
Dmitry Noskov Spring Framework - Validation
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

Questions
Dmitry Noskov Spring Framework - Validation

The end
http://www.linkedin.com/in/noskovd
http://www.slideshare.net/analizator/presentations