【问题标题】:Non bean property throwing Bean property is not readable or has an invalid getter method非 bean 属性抛出 Bean 属性不可读或具有无效的 getter 方法
【发布时间】:2020-11-27 02:11:37
【问题描述】:

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

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

org.springframework.beans.NotReadablePropertyException: 无效 bean 类 [com.Alex.UserPackage.User] 的属性 'Yijian@123': Bean 属性 'Yijian@123' 不可读或有无效的 getter 方法: getter 的返回类型是否与 getter 的参数类型匹配 二传手?在 org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:612) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]

实体类

@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 自定义注解。在我使用 getter 获取密码和匹配密码后,错误开始发生

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>

【问题讨论】:

    标签: spring-boot hibernate spring-mvc annotations spring-annotations


    【解决方案1】:

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

    替换以下内容:

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

    与:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 2019-12-13
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多