简体   繁体   中英

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?

I have tried using th:field="*{owner}" ,

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

th:field="*{setOwner}" but still gets the same error.

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

You did mistake while creating your Getter & Setter for your Entity class. 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. 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.

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.

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 is a part of the spring-boot starter as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question 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 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 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 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 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 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? 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 Using @ManyToOne value in Thymeleaf template - property '' is not readable or has an invalid getter method Does the return type of the getter Bean property is not readable or has an invalid getter method Does the parameter type of the setter match the return type of the getter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM