「Spring MVC」Validationのアノテーションを定義するメモ
1.NotNull.java
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER })
@Constraint(validatedBy = { NotNullValidator.class })
@Retention(RetentionPolicy.RUNTIME)
public @interface NotNull {
String field() default “";
String message() default “{field.can.not.be.null}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
2.ConstraintValidatorインタフェースの実現
NotNullValidator.java
public class NotNullValidator implements ConstraintValidator<NotNull, Object> {
@Override
public void initialize(NotNull annotation) {
}
@Override
public boolean isValid(Object str, ConstraintValidatorContext constraintValidatorContext) {
return str != null;
}
}