While working with the Spring boot project, you might face the error “Invalid property of bean class is not readable or has an invalid getter method in spring boot”.
The complete error message that you can see in the console is:
Invalid property ‘firstName’ of bean class [com.schoolmgt.form.UserForm]:
Bean property ‘firstName’ is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
In this Blog, I will show you how can solve this spring boot error in some simple steps. there are two main causes of the error first,
- Did not define proper getter, and setter into your spring forms.
- Verify the form path name is mapped with the same name in spring forms.
Check your form class has Getter, Setter
Open your related spring form class and check you have getters and setters for the specific parameter, If you are using Lombok make sure you have added the annotation at the top of the form class as shown in the below example.
@Getter @Setter public class UserForm extends BaseDTO { }
In case you are not using the Lombok plugin then create Getter and Setter manually as shown in the below code snippet.
public class UserForm extends BaseDTO { @NotEmpty(message = "First name is required") private String firstName; @NotEmpty(message = "Last name is required") private String lastName; @NotEmpty(message = "DOB name is required") private String dob; @NotEmpty(message = "First name is required") private String gender; @NotEmpty(message = "First name is required") private String email; @NotEmpty(message = "First name is required") private String password; @NotEmpty(message = "First name is required") private String phoneNumber; @NotEmpty(message = "User Role is required") private String userRole; private String operation; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getDob() { return dob; } public void setDob(String dob) { this.dob = dob; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getUserRole() { return userRole; } public void setUserRole(String userRole) { this.userRole = userRole; } public String getOperation() { return operation; } public void setOperation(String operation) { this.operation = operation; } public UserDTO getDTO() { UserDTO bean=new UserDTO(); bean.setId(id); bean.setFirstName(firstName); bean.setLastName(lastName); bean.setDob(dob); bean.setGender(gender); bean.setEmail(email); bean.setPassword(password); bean.setPhoneNumber(phoneNumber); bean.setUserRole(userRole); return bean; } public void populate(UserDTO bean) { id = bean.getId(); firstName=bean.getFirstName(); lastName=bean.getLastName(); dob = bean.getDob(); gender = bean.getGender(); email = bean.getEmail(); password = bean.getPassword(); phoneNumber = bean.getPhoneNumber(); userRole = bean.getUserRole(); } }
Check the mapped element name or path should same as the defined variable in from class
Check the defined path name in JSP or HTML form and match it with the variable name that you have defined in the form class.
It’s mapped automatically by spring boot, so if it will not be the same then spring will throw the error “Invalid property of bean class is not readable or has an invalid getter method in spring boot”.
Path name into JSP form
<s:bind path="firstName"> <font color="red" style="font-size: 15px"><sf:errors path="${status.expression}" /></font> <sf:input type="text" id="firstName" class="form-control form-control-lg" path="firstName" name="firstName"/> </s:bind>
Entity property or variable name into spring boot form class
@NotEmpty(message = "First name is required") private String firstName;
Observe the above 2 code snippet the private String firstName;
is the same as the spring boot form class variable and JSP from the input path name that bind by spring forms.