使用javax.validation进行自定义验证



我正在尝试制作一个自定义的java验证注释,并返回

请求处理失败;嵌套异常是javax.validation.ConstationDeclarationException: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;

更改

@SupportedValidationTarget(ValidationTarget.PARAMETERS)

@SupportedValidationTarget(ValidationTarget.ANNOTATED_ELEMENT)

因为您想要验证和元素(这里是列表列表(而不是方法或构造函数的参数

相关内容

  • 没有找到相关文章

最新更新