非 bean 属性 抛出 Bean 属性 不可读或具有无效的 getter 方法

Non bean property throwing Bean property is not readable or has an invalid getter method

所以我的问题与其他问题有点不同。它抛出的错误不是字段名称,而是表单中的输入。我以前从未遇到过这个错误

错误。 ''的内容就是我输入的密码

org.springframework.beans.NotReadablePropertyException: Invalid property 'Yijian@123' of bean class [com.Alex.UserPackage.User]: Bean property 'Yijian@123' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:612) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]

实体class

@Entity
@ValidPassword
public class User {
    @Pattern(regexp="[a-zA-Z]+", message = "Enter letters only!")
    private String firstName;
    @Pattern(regexp="[a-zA-Z]+", message = "Enter letters only!")
    private String lastName;
    private String password;
    private String matchingPassword;
    private String passportNumber;


    

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getMatchingPassword() {
        return matchingPassword;
    }

    public void setMatchingPassword(String matchingPassword) {
        this.matchingPassword = matchingPassword;
    }


}

@ValidPassword 自定义注释。在我使用密码和匹配密码

的吸气剂后开始出现错误
private String message;

@Override
public boolean isValid(User user, ConstraintValidatorContext context) {
    String password = user.getPassword();
    String matchingPassword = user.getMatchingPassword();
    if (password== null || matchingPassword == null) {
    return false;
    }
    
    System.out.println("PASSWORDS: " + password + matchingPassword);
    
    boolean flag = Pattern.matches("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$", password);
    boolean flag1 = password.equals(matchingPassword);
    
    if ( !flag1 ) {
        message = "Passwords do not match!";
    }
    
    
     context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(message)
                .addPropertyNode(password).addConstraintViolation();
        
    return flag && flag1;
    
}

//Show default message if no special message is set
@Override
public void initialize(ValidPassword validPassword) {
    
     message = validPassword.message();
}

表单密码部分

<div class = "row">
            <div class="col-sm-6 form-group">
                <label>Password : </label> <Input type="password"
                    th:field="*{password}" th:required="required" class="form-control" />
                    <p th:if="${#fields.hasErrors('password')}"
                    th:errors="*{password}" class="alert alert-danger"></p>
            </div>

您正在将 属性 值而不是 属性 名称传递给 addPropertyNode(password)

替换以下内容:

context.buildConstraintViolationWithTemplate(message)
                .addPropertyNode(password).addConstraintViolation();

与:

context.buildConstraintViolationWithTemplate(message)
                .addPropertyNode("password").addConstraintViolation();