deng A (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5e6cb37...
) *created* an issue
Hibernate Validator (
https://hibernate.atlassian.net/browse/HV?atlOrigin=eyJpIjoiYWFlYzBjYzJjN...
) / Bug (
https://hibernate.atlassian.net/browse/HV-1761?atlOrigin=eyJpIjoiYWFlYzBj...
) HV-1761 (
https://hibernate.atlassian.net/browse/HV-1761?atlOrigin=eyJpIjoiYWFlYzBj...
) Not Work!! When i use custom validation,ALL version!!! (
https://hibernate.atlassian.net/browse/HV-1761?atlOrigin=eyJpIjoiYWFlYzBj...
)
Issue Type: Bug Affects Versions: 6.0.18.Final, 6.1.2.Final Assignee: Unassigned
Attachments: 微信图片_20200314190706.png Components: engine Created: 14/Mar/2020 04:09 AM
Environment: spring-boot-2.2.5
jdk11
spring-boot-starter-validation
Labels: validation Priority: Blocker Reporter: deng A (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5e6cb37...
) Original Estimate: 32h Remaining Estimate: 32h
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
/*
* 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();
}
}
/*
* 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 ;
}
}
*controller*
/**
* 自定义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;
}
messages.properties
javax.validation.constraints.IntEnum.message=value not valid(valid values have {value})
error!!!
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
(
https://hibernate.atlassian.net/browse/HV-1761#add-comment?atlOrigin=eyJp...
) Add Comment (
https://hibernate.atlassian.net/browse/HV-1761#add-comment?atlOrigin=eyJp...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100121- sha1:a2b3dcc )