简体   繁体   English

Bean 属性 'xxx' 不可读或具有无效的 getter 方法:getter 的返回类型是否与 setter 的参数类型匹配?

[英]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?

I keep getting this error and now I'm not sure why.我一直收到这个错误,现在我不知道为什么。

Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'owner' of bean class [java.util.ArrayList]: Bean property 'owner' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?引起:org.springframework.beans.NotReadablePropertyException: Invalid property 'owner' of bean class [java.util.ArrayList]: Bean property 'owner' 不可读或具有无效的 getter 方法:getter 的返回类型是否匹配setter 的参数类型?

I have tried using th:field="*{owner}" ,我试过使用th:field="*{owner}"

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

th:field="*{setOwner}" but still gets the same error. th:field="*{setOwner}"但仍然得到同样的错误。

Controller控制器

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

Model模型

@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; 
}
}

thymeleaf百里香叶

<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>

you have written the code for getOwner wrongly.您错误地编写了 getOwner 的代码。 The data type of the owner is string and you have declared the method as long.所有者的数据类型是字符串,并且您已经声明了该方法。 please rewrite the code to following.请将代码重写为以下内容。

public String getOwner() { 
return owner; 
}

TIP - Use a IDE while coding, it will help you to figure these compile time errors easily.提示 - 在编码时使用 IDE,它将帮助您轻松解决这些编译时错误。

You did mistake while creating your Getter & Setter for your Entity class.您在为实体类创建 Getter 和 Setter 时犯了错误。 and How your program compiled successfully.以及您的程序如何成功编译。 Because inside因为里面

public long getOwner() { 
    return owner; 
}

Method you are using long as a return type but returning String from the getter.您使用 long 作为返回类型但从 getter 返回 String 的方法。 Rewrite your class like this像这样重写你的类

@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; 
}
}

Or you can choose any IDE ex: Eclipse, IntelliJ for writing the class.或者您可以选择任何 IDE,例如:Eclipse、IntelliJ 来编写类。

Problem fixed!问题已解决! The controller was supposed to be for listing customer.控制器应该用于列出客户。 Changed it to改成

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

Thanks everyone!谢谢大家!

Incorrect getters/setters in POJO. POJO 中的 getter/setter 不正确。

A classic example of why to make code more readable.为什么要使代码更具可读性的经典示例。

It happens because of unnecessary boiler-plate redundant code.这是因为不必要的样板冗余代码。

Use this-(LOMBOK) dependency/jar and all of the code which reduces readability would be removed.使用这个-(LOMBOK)依赖/jar,所有降低可读性的代码都将被删除。 Lombok is a part of the spring-boot starter as well. Lombok 也是 spring-boot starter 的一部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Bean属性“ cmpcode”不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配? - Bean property 'cmpcode' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? Bean属性&#39;xxxx不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配 - Bean property 'xxxx is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter Spring MVC:Bean属性不可读或具有无效的getter方法getter的返回类型是否与setter的参数类型匹配 - Spring MVC: Bean property is not readable or has an invalid getter method Does the return type of the getter match the parameter type of the setter Bean属性不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配? 春季批 - Bean property is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? Spring Batch Bean属性“ trustStore”不可写或具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配? - Bean property 'trustStore' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? Bean 属性 'sakilaDao' 不可写或具有无效的 setter 方法。 setter 的参数类型是否与 getter 的返回类型匹配? - Bean property 'sakilaDao' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? Bean类的无效属性&#39;id&#39;:Bean属性&#39;id&#39;不可读或具有无效的getter方法:getter的返回类型是否匹配 - Invalid property 'id' of bean class: Bean property 'id' is not readable or has an invalid getter method: Does the return type of the getter match 在 Thymeleaf 模板中使用 @ManyToOne 值 - 属性 '' 不可读或具有无效的 getter 方法 - Using @ManyToOne value in Thymeleaf template - property '' is not readable or has an invalid getter method Does the return type of the getter Bean属性不可读或具有无效的getter方法 - Bean property is not readable or has an invalid getter method setter的参数类型是否与getter的返回类型匹配? - Does the parameter type of the setter match the return type of the getter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM