i learn springboot validation with hibernate-validator recently. i write some custom validator however it not work when i custom message i want to valid a enum flag
relation class:
[https://github.com/hibernate/hibernate - validator/blob/master/engine/src/main/java/org/hibernate/validator/internal/engine/messageinterpolation/ParameterTermResolver.java|https://github.com/hibernate/hibernate - validator/blob/master/engine/src/main/java/org/hibernate/validator/internal/engine/messageinterpolation/ParameterTermResolver.java|smart - link]
- ---
{code:java} /* * Copyright (c) 2020. ascend 版权所有禁止转载。 */
package cn.edu.zua.springbootdemo2.util.core.validation;
import cn.edu.zua.springbootdemo2.util.core.validation.bound.IntEnumValidatorForInteger;
import javax.validation.Constraint; import javax.validation.Payload; import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.RUNTIME;
/** * 说明:The annotated element must be a number of values . * * @author adeng 2020/3/14 10:12 */ @Documented @Constraint(validatedBy = {IntEnumValidatorForInteger.class}) @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) @Repeatable(IntEnum.List.class) public @interface IntEnum { /** * 合法值 */ int[] value() default {};
String message() default "{javax.validation.constraints.IntEnum.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/** * Defines several {@code @Email} constraints on the same element. * * @see IntEnum */ @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) @Retention(RUNTIME) @Documented @interface List { IntEnum[] value(); } } {code}
----
{code:java} /* * Copyright (c) 2020. ascend 版权所有禁止转载。 */
package cn.edu.zua.springbootdemo2.util.core.validation.bound;
import cn.edu.zua.springbootdemo2.util.core.validation.IntEnum;
import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext;
/** * 说明: * 验证提供的值是否合法 * * @author adeng 2020/3/14 15:21 */ public class IntEnumValidatorForInteger implements ConstraintValidator<IntEnum, Integer> { /** * 合法值 */ private int[] values;
/** * Initializes the validator in preparation for * {@link #isValid(Integer, ConstraintValidatorContext)} calls. * The constraint annotation for a given constraint declaration * is passed. * <p> * This method is guaranteed to be called before any use of this instance for * validation. * <p> * The default implementation is a no-op. * * @param intEnum annotation instance for a given constraint declaration */ @Override public void initialize(IntEnum intEnum) { this.values = intEnum.value(); }
/** * Implements the validation logic. * The state of {@code value} must not be altered. * <p> * This method can be accessed concurrently, thread-safety must be ensured * by the implementation. * * @param value object to validate * @param context context in which the constraint is evaluated * @return {@code false} if {@code value} does not pass the constraint */ @Override public boolean isValid(Integer value, ConstraintValidatorContext context) { for (int item : values) { if (value == item) { return true; } } return false; } } {code}
----
*controller*
{code:java} /** * 自定义int枚举校验 * * @param input 输入 * @return 输出 */ @GetMapping(value = "/get/custom/integer") public CommonResult<Integer> getCustomInteger(@IntEnum(value = {0, 1}) Integer input) { CommonResult<Integer> retResult = new CommonResult<>(); retResult.fillResult(CommonCode.SUCCESS(), input); return retResult; } {code}
---- messages.properties
{code:java} javax.validation.constraints.IntEnum.message=value not valid(valid values have {value}) {code}
---- error \ ! \ ! \ ! {code:java} Caused by: java.lang.ClassCastException: class [I cannot be cast to class [Ljava.lang.Object; ([I and [Ljava.lang.Object; are in module java.base of loader 'bootstrap') at org.hibernate.validator.internal.engine.messageinterpolation.ParameterTermResolver.interpolate(ParameterTermResolver.java:30) at org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTerm.interpolate(InterpolationTerm.java:64) at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolate(ResourceBundleMessageInterpolator.java:159) at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolateExpression(AbstractMessageInterpolator.java:519) at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolateMessage(AbstractMessageInterpolator.java:408) at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolate(AbstractMessageInterpolator.java:355) at org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator.interpolate(LocaleContextMessageInterpolator.java:51) at org.hibernate.validator.internal.engine.validationcontext.AbstractValidationContext.interpolate(AbstractValidationContext.java:313) ... 75 common frames omitted {code}
|
|