简体   繁体   中英

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

Now an working on java using springframework springBootVersion = '1.5.9.RELEASE' but every time i run my web app it gives me

org.springframework.beans.NotReadablePropertyException: Invalid property 'id' of bean class [java.util.ArrayList]: Bean property 'id' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:631) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:149) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:401) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE]

my User class is

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class User {

@Id
private int id;
private String name;
private int age;

public User() {
}

public User(int id, String name, int age) {
    this.id = id;
    this.name = name;
    this.age = age;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
}

my controller class is

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class UserController {

@Autowired
private UserService userService;

@RequestMapping(value = "/")
public String display(ModelMap modelMap){
    User user = new User(1, "Endriyas", 21);
    userService.addUser(user);
    List<User> users = userService.getAllUser();
    modelMap.put("users", users);
    return "users";
}

}

and my HTML form is

<div>
    <form action="#" th:action="@{/}" th:object="${users}" method="post">
        <div>
            <label>
                <span>ID</span>
            </label>
            <input type="number" th:field="*{id}"/>
        </div>
        <div>
            <label>
                <span>Name</span>
            </label>
            <input type="text" th:field="*{name}"/>
        </div>
        <div>
            <label>
                <span>Age</span>
            </label>
            <input type="number" th:field="*{age}"/>
        </div>
        <div>
            <button type="submit" name="save">SAVE</button>
        </div>
    </form>
</div>

Please help me ..... I need help

Thanks

you are passing a list of users (in the controller) but actually in the form you want to save one user.

In order just to display the new created user (with the default values)

change the code to

 @RequestMapping(value = "/")
public String display(ModelMap modelMap){
    User user = new User(1, "Endriyas", 21);
    userService.addUser(user);
    User users = userService.getById(1);
    modelMap.put("users", users);
    return "users";
}

In addition, you should create a new method in the service

getById (that has an Integer as a parameter)

Is the addUser method save the user in the database? If not and you just want to display the user with some default values change the code to this

@RequestMapping(value = "/")
    public String display(ModelMap modelMap){
        User user = new User(1, "Endriyas", 21);
        modelMap.put("users", user);
        return "users";
    }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM