我正在尝试在春季引导中实施自定义密码匹配验证。但我收到如下错误:-
PasswordMatch包含约束注释,但不包含消息参数
我正在跟踪此链接https://www.baeldung.com/registration-with-spring-mvc-and-spring-security进行自定义验证。问题是这样我出错了。
javax.validation.ConstraintDefinitionException:HV000074:com.bikram.booking.validation.PasswordMatch包含约束注释,但不包含消息参数。 在org.hibernate.validator.internal.metadata.core.ConstraintHelper.assertMessageParameterExists(ConstraintHelper.java:915)
我已经在网上搜索了解决方案,但是找不到像样的灵魂。
我的模态是
package com.bikram.booking.dto;
import com.bikram.booking.validation.PasswordMatch;
import com.bikram.booking.validation.ValidEmail;
import javax.validation.constraints.*;
@PasswordMatch
public class UserDto {
@NotNull
@Size(min = 6, message = "Password should be more than 6 characters")
@NotEmpty(message = "Please provide a password")
private String password;
@NotNull
@Size(min = 6, message = "Password should be more than 6 characters")
private String confirmPassword;
}
我的界面是
package com.bikram.booking.validation;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target({ TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = PasswordMatchValidator.class)
@Documented
public @interface PasswordMatch {
String messages() default "Sorry, passwords does not match";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
实施是
package com.bikram.booking.validation;
import com.bikram.booking.dto.UserDto;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class PasswordMatchValidator implements ConstraintValidator<PasswordMatch, Object> {
@Override
public void initialize(PasswordMatch constraintAnnotation) {
}
@Override
public boolean isValid(Object obj, ConstraintValidatorContext constraintValidatorContext) {
UserDto userDto = (UserDto) obj;
return true;
}
}
任何提示都是很明显的。
答案 0 :(得分:1)
在messages()
中将message()
重命名为PasswordMatch
:
public @interface PasswordMatch {
String message() default "Sorry, passwords does not match";
....
}