Mình đã thêm thư viện Lombok vào chương trình, nhưng khi chạy nó lại báo lỗi không nhận được getter.
Mình đã sử dụng phần mềm eclipse có springtool
Đây là lỗi khi mình chạy phần mềm
An error happened during template parsing (template: "class path resource [templates/add.html]").
Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "add" - line 10, col 40).
Invalid property 'id' of bean class [com.example.demo.models.Book]: 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?
Class Entity
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Table(name = "book")
@Entity
@NoArgsConstructor
@Getter
@Setter
@AllArgsConstructor
public class Book implements Serializable{
@Id
private Long id;
@Column
private String name;
@Column
private String image;
}
Còn đây là class template của mình
<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Insert Book</title>
</head>
<body>
<form th:action="@{/save}" th:object="${book}" method="post">
<div th:if="${id == null}">
<input type="text" placeholder="Id" th:field="*{id}" /><br />
</div>
<input type="text" placeholder="Name" th:field="*{name}" /><br /> <input
type="text" th:field="*{image}" placeholder="Image" /><br /> <input
type="submit" value="Confirm"> <a th:href="@{/list}">List</a>
</form>
</body>
</html>
Class Controller
@Controller
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("/")
public String add(ModelMap model) {
model.addAttribute("book", new Book());
return "add";
}
@PostMapping("/save")
public String save(ModelMap model, Book book) {
bookService.save(book);
model.addAttribute(book);
return list(model);
}
@GetMapping("/list")
public String list(ModelMap model) {
List<Book> list = (List<Book>) bookService.findAll();
model.addAttribute("books", list);
return "list";
}
}
Khi mình getter/setter bằng thủ công thì chạy được nhưng khi sử dụng Lombok thì phát sinh lỗi khi chạy