我正在尝试创建一个自定义的java验证注释,并返回给我
请求处理失败;嵌套异常为javax.validation.ConstraintDeclarationException: HV000144:交叉参数约束com.my.company.CustomConstraint非法放置在字段‘javax.validation.ConstraintDeclarationException: java.util.List com.my.company.ElementOfTheList’上。“
代码实在是太幼稚了
@Documented
@Retention(RUNTIME)
@Target({ FIELD, METHOD})
@Constraint(validatedBy = ConstraintValidation.class)
public @interface CustomConstraint {
String message() default "this is the default message";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}@SupportedValidationTarget(ValidationTarget.PARAMETERS)
public class ConstraintValidationimplements ConstraintValidator<CustomConstraint , List<ElementOfTheList>> {
public boolean isValid(List<ElementOfTheList> value, ConstraintValidatorContext context) {
System.out.println("only a sysout to test");
return true;
}
}在rest对象模型中
@JsonProperty("ElementOfTheList")
@Valid
@NotNull(message ="not null message")
@NotEmpty(message = "not empty message")
@CustomConstraint
private List<ElementOfTheList> list = null;发布于 2020-08-12 23:45:09
变化
@SupportedValidationTarget(ValidationTarget.PARAMETERS)至
@SupportedValidationTarget(ValidationTarget.ANNOTATED_ELEMENT)因为您希望验证and元素(这里是List list),而不是方法或构造函数的参数
https://stackoverflow.com/questions/63373437
复制相似问题