gpt4 book ai didi

java - Spring MVC 自定义验证显示错误代码 HV000074。

转载 作者:行者123 更新时间:2023-12-02 01:55:40 24 4
gpt4 key购买 nike

我正在尝试在 Spring Boot 中实现自定义密码匹配验证。但我收到如下错误:-

PasswordMatch包含Constraint注释,但不包含消息参数

我正在关注此链接 https://www.baeldung.com/registration-with-spring-mvc-and-spring-security用于自定义验证。问题是我收到这样的错误。

javax.validation.ConstraintDefinitionException: HV000074: com.bikram.booking.validation.PasswordMatch 包含 Constraint 注释,但不包含消息参数。 在 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;
}
}

任何提示都将非常值得赞赏。

最佳答案

PasswordMatch中将messages()重命名为message():

public @interface PasswordMatch {
String message() default "Sorry, passwords does not match";
....
}

关于java - Spring MVC 自定义验证显示错误代码 HV000074。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52352651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com