【发布时间】:2020-08-12 23:45:08
【问题描述】:
我正在尝试制作自定义 java 验证注释并返回我
请求处理失败;嵌套异常是 javax.validation.ConstraintDeclarationException: HV000144: 跨参数约束 com.my.company.CustomConstraint 被非法放置在字段 'private 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;
}
}
在其余对象模型中
@JsonProperty("ElementOfTheList")
@Valid
@NotNull(message ="not null message")
@NotEmpty(message = "not empty message")
@CustomConstraint
private List<ElementOfTheList> list = null;
【问题讨论】:
标签: java spring validation