Suppose, I have a model like below -
public class A{
public List<String> phoneNumbers;
}
public List<String> phoneNumbers;
}
I want to apply @Max validation, so as to not allow phoneNumbers more then a certain limit. And, it is required to have this limit configurable in properties file.
By Default, @Max takes static values. We can not pass value read from application.properties to this annotation. So, we can write custom annotation like below -
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = FieldValidator.class)
public @interface MaxAllowedSize {
String message() default "size of list must be less than or equal to %s";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = FieldValidator.class)
public @interface MaxAllowedSize {
String message() default "size of list must be less than or equal to %s";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
---------
--------
Validator can be written as below -
import java.util.List;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.springframework.beans.factory.annotation.Value;
/**
* Validate list of phone numbers
*
*/
public class FieldValidator implements ConstraintValidator<MaxAllowedSize, List<String>> {
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.springframework.beans.factory.annotation.Value;
/**
* Validate list of phone numbers
*
*/
public class FieldValidator implements ConstraintValidator<MaxAllowedSize, List<String>> {
// Read value from application.properties
@Value("${allowed.phn.count}")
private int maxAllowedPhNSize;
@Value("${allowed.phn.count}")
private int maxAllowedPhNSize;
@Override
public boolean isValid(List<String> value, ConstraintValidatorContext context) {
if (value == null || (maxAllowedPhNSize > 0 && value.size() > maxAllowedPhNSize)) {
formatMessage(context);
return false;
}
return true;
}
/**
* format message to add phone number allowed size
* @param context
*/
private void formatMessage(ConstraintValidatorContext context) {
String msg = context.getDefaultConstraintMessageTemplate();
String formattedMsg = String.format(msg, this.maxAllowedPhNSize);
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(formattedMsg).addConstraintViolation();
}
}
----
----
Model can be annotated as below -
public class A{
@MaxAllowedSize
public List<String> phoneNumbers;
public List<String> phoneNumbers;
}
Comments
Post a Comment