【问题标题】: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 matchbean 类的无效属性“id”:bean 属性“id”不可读或具有无效的 getter 方法:getter 的返回类型是否匹配
【发布时间】:2018-08-30 20:34:58
【问题描述】:

现在我正在使用 spring 框架 spring boot version = '1.5.9.RELEASE' 开发 java。但每次我运行我的网络应用程序时,它都会给我

org.springframework.beans.NotReadablePropertyException: bean 类 [java.util.ArrayList] 的无效属性“id”:bean 属性“id”不可读或有无效的 getter 方法:getter 的返回类型是否匹配设置器的参数类型? 在 org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:631) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.web.servlet.support.BindStatus.(BindStatus.java:149) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:401) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE]

我的用户类是

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

我的控制器类是

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

}

我的 HTML 表单是

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

我需要帮助

谢谢

【问题讨论】:

  • 你想输出User对象还是相反?
  • 我的浏览器告诉我,在哪里输入有 id 有错误和 ide 打印 org.springframework.beans.NotReadablePropertyException: Invalid property 'id' of bean class [java.util.ArrayList]: Bean 属性 'id' 不可读或有无效的 getter 方法:getter 的返回类型是否与 setter 的参数类型匹配?
  • id的类型不是“长”了吗?
  • 为什么这么长?我不明白你的意思

标签: java spring-boot thymeleaf javabeans


【解决方案1】:

您正在传递一个用户列表(在控制器中),但实际上是以您想要保存一个用户的形式。

为了只显示新创建的用户(使用默认值)

把代码改成

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

另外,你应该在服务中创建一个新方法

getById (that has an Integer as a parameter)

addUser 方法是否将用户保存在数据库中? 如果没有,您只想向用户显示一些默认值 把代码改成这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2014-10-17
    • 2019-12-20
    相关资源
    最近更新 更多