To write a @interfaceclass, I use ValidValue as an example here.
The following three are required JSR303in the regulations
// 校验不通过时返回的错误信息
String message()default"The specified value must be submitted";// 在需要多个校验规则的时候可以指定是哪个组的校验规则
Class<?>[]groups()default{
};//
Class<?extendsPayload>[]payload()default{
};
The set rules are the rules int[] vals() default {};you want to use for verification. This means that the number you enter must be in the array.
The complete example is as follows ( if you don't know what the annotations on the class use, you can just click in a check annotation and copy it )
The ValidValueConstraintValidatorvalidator inside (described below) needs to be bound to your validation annotations
{
// 这里就是你自定义的默认返回错误"showStatus":"The specified value must be submitted",}
5. How to customize the error message returned
In the comments provided by the jar package, their error messages are all placed in the jar package, which can be seen by ValidationMessages.propertiesglobal search ValidationMessages.
E.g
javax.validation.constraints.AssertFalse.message = must be false
javax.validation.constraints.AssertTrue.message = must be true
javax.validation.constraints.DecimalMax.message = must be less than ${
inclusive ==true?'or equal to ':''}{
value}
javax.validation.constraints.DecimalMin.message = must be greater than ${
inclusive ==true?'or equal to ':''}{
value}
javax.validation.constraints.Digits.message = numeric value out of bounds (<{
integer} digits>.<{
fraction} digits> expected)
javax.validation.constraints.Email.message = must be a well-formed email address
javax.validation.constraints.Future.message = must be a future date
javax.validation.constraints.FutureOrPresent.message = must be a date in the present or in the future
javax.validation.constraints.Max.message = must be less than or equal to {
value}
javax.validation.constraints.Min.message = must be greater than or equal to {
value}
javax.validation.constraints.Negative.message = must be less than 0
javax.validation.constraints.NegativeOrZero.message = must be less than or equal to 0
javax.validation.constraints.NotBlank.message = must not be blank
javax.validation.constraints.NotEmpty.message = must not be empty
javax.validation.constraints.NotNull.message = must not be null
So we write a ValidationMessages.propertiesfile ourselves and put it in the resources directory
com.study.common.valid.ValidValue.messageIs your custom annotation plus.message