【问题标题】:Bean property 'xxx' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?Bean 属性 'xxx' 不可读或有无效的 getter 方法:getter 的返回类型是否与 setter 的参数类型匹配?
【发布时间】:2019-12-20 19:46:36
【问题描述】:

我不断收到此错误,现在我不知道为什么。

原因:org.springframework.beans.NotReadablePropertyException:bean 类 [java.util.ArrayList] 的无效属性“所有者”:bean 属性“所有者”不可读或具有无效的 getter 方法:返回类型是否为getter 是否匹配 setter 的参数类型?

我尝试过使用 th:field="*{owner}",

th:field="*{Owner}",和

th:field="*{setOwner}" 但仍然出现同样的错误。

控制器

@RequestMapping("/wqrms/customer/create")
public String customerCreate(Model model) {
    List<Customer> customer = customerService.listAll();
    model.addAttribute("customer", customer);
    return "/views/wqrms/customer/create";
}

型号

@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String owner;
public long getId() { 
    return id; 
}
public void setId(long id) {
    this.id = id; 
}
public String getOwner() { 
    return owner; 
}
public void setOwner(long id) {
    this.owner = owner; 
}
}

百里香

<form action="#" th:action="@{/wqrms/customer/save}" th:object="${customer}" method="post">     
        <div class="form-row">
            <div class="form-group col-md-6">
                <label>Customer Name</label> 
                <input class="form-control" placeholder="Customer Name" required th:field="*{owner}">
            </div>

【问题讨论】:

  • 属性'customer'的类型是List,它确实没有字段'owner'。您可能希望向模型中添加一个“空”的单个客户,然后将其填充为 setter 方法。

标签: java html spring spring-boot thymeleaf


【解决方案1】:

问题已解决!控制器应该用于列出客户。改成

`@RequestMapping("/wqrms/customer/create")
 public String customerCreate(Model model) {
     Customer customer = new Customer();
     model.addAttribute("customer", customer);
     return "/views/wqrms/customer/create";
}`

谢谢大家!

【讨论】:

    【解决方案2】:

    POJO 中的 getter/setter 不正确。

    为什么要使代码更具可读性的经典示例。

    这是因为不必要的样板冗余代码。

    使用this-(LOMBOK)dependency/jar 并且所有降低可读性的代码都将被删除。 Lombok 也是 spring-boot 启动器的一部分。

    【讨论】:

    • 在我的项目中,我们使用的是 Lombok @mossad,这种威胁与此无关。提到龙目岛很好,但你的回答没有详细解释为什么会发生这种情况以及它来自哪里......并且不要在 stakoverflow 中有毒 :)
    【解决方案3】:

    您在为实体类创建 Getter 和 Setter 时犯了错误。以及您的程序如何成功编译。因为在里面

    public long getOwner() { 
        return owner; 
    }
    

    您使用 long 作为返回类型但从 getter 返回 String 的方法。 像这样重写你的类

    @Entity
    public class Customer {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String owner;
    
    public long getId() { 
        return id; 
    }
    public void setId(long id) {
        this.id = id; 
    }
    public String getOwner() { 
        return owner; 
    }
    public void setOwner(String owner) {
        this.owner = owner; 
    }
    }
    

    或者您可以选择任何 IDE 例如:Eclipse、IntelliJ 来编写类。

    【讨论】:

    • 我确实将 long 更改为 String 并且仍然得到相同的错误。我已经在eclipse中重写了代码,但仍然没有运气。
    • 更改 Getter & Setter 后。请清理您的项目一次。
    【解决方案4】:

    您错误地编写了 getOwner 的代码。所有者的数据类型是字符串,并且您已经声明了该方法。 请将代码改写为以下。

    public String getOwner() { 
    return owner; 
    }
    

    提示 - 在编码时使用 IDE,它将帮助您轻松解决这些编译时错误。

    【讨论】:

    • 刚刚使用了eclipse并生成了getter和setter。我确实将 long 更改为 String。这是一个错字,但仍然出现同样的错误。
    猜你喜欢
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多