0
votes

Now I'm working on java using spring framework spring boot version = '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>

I need help

Thanks

1
Do you want to output the User object or opposite?Sergey Prokofiev
my browser tells me that in line where the input having id have error and the ide prints 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?Endriyas
Hasn't the type of the id to be "long"?watchme
why long? i don't understand your pointEndriyas

1 Answers

1
votes

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