自定义验证与javax.validation

huangapple go评论65阅读模式
英文:

Custom validation with javax.validation

问题

Request processing failed; nested exception is javax.validation.ConstraintDeclarationException: HV000144: Cross parameter constraint com.my.company.CustomConstraint is illegally placed on field '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 ConstraintValidation implements ConstraintValidator<CustomConstraint, List<ElementOfTheList>> {

	public boolean isValid(List<ElementOfTheList> value, ConstraintValidatorContext context) {
		System.out.println("only a sysout to test");
		return true;
	}
}

And in the rest object model

@JsonProperty("ElementOfTheList")
@Valid
@NotNull(message = "not null message")
@NotEmpty(message = "not empty message")
@CustomConstraint
private List<ElementOfTheList> list = null;
英文:

I'm trying to make a custom java validation annotation and returns me

Request processing failed; nested exception is javax.validation.ConstraintDeclarationException: HV000144: Cross parameter constraint com.my.company.CustomConstraint is illegally placed on field 'private java.util.List com.my.company.ElementOfTheList'."

the code is really naive

@Documented
@Retention(RUNTIME)
@Target({ FIELD, METHOD})
@Constraint(validatedBy = ConstraintValidation.class)
public @interface CustomConstraint {

	String message() default &quot;this is the default message&quot;;

	Class&lt;?&gt;[] groups() default {};

	Class&lt;? extends Payload&gt;[] payload() default {};
}
@SupportedValidationTarget(ValidationTarget.PARAMETERS)
public class ConstraintValidationimplements ConstraintValidator&lt;CustomConstraint , List&lt;ElementOfTheList&gt;&gt; {

	public boolean isValid(List&lt;ElementOfTheList&gt; value, ConstraintValidatorContext context) {
		System.out.println(&quot;only a sysout to test&quot;);
		return true;
	}
}

And in the rest object model

  @JsonProperty(&quot;ElementOfTheList&quot;)
  @Valid
  @NotNull(message =&quot;not null message&quot;)
  @NotEmpty(message = &quot;not empty message&quot;)
  @CustomConstraint 
  private List&lt;ElementOfTheList&gt; list = null;

答案1

得分: 4

将以下部分进行翻译:

@SupportedValidationTarget(ValidationTarget.PARAMETERS)

更改为

@SupportedValidationTarget(ValidationTarget.ANNOTATED_ELEMENT)

因为您想要验证一个元素(这里是 List list ),而不是方法或构造函数的参数。

英文:

change

@SupportedValidationTarget(ValidationTarget.PARAMETERS)

to

@SupportedValidationTarget(ValidationTarget.ANNOTATED_ELEMENT)

since you want to validate and element (here is List<ElementOfTheList> list)
and not the parameters of a method or a constructor

huangapple
  • 本文由 发表于 2020年8月12日 17:14:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63373437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定