При добавлении записи возникает 500
Вот проперти (у меня на MySQL):
spring.datasource.url=jdbc:mysql://localhost:3306/pavilion
spring.datasource.username=pavilioner
spring.datasource.password=12345678
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
Вот контроллер:
@AllArgsConstructor
@Controller
public class AnimalController {
private final AnimalService service;
@GetMapping("/")
public String index(Model model) {
model.addAttribute("animals", service.getAll());
model.addAttribute("title", "Главная");
return "index";
}
@GetMapping("/add")
public String add(Model model) {
return "add";
}
@PostMapping("/add")
public String save(@RequestParam String name, @RequestParam String poroda, @RequestParam String age, Model model) {
Animal animal = new Animal();
animal.setName(name);
animal.setAge(age);
animal.setPoroda(poroda);
service.save(animal);
return "redirect:/";
}
}
Вот HTML c Thymeleaf:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div><h2>Добавить животное</h2></div>
<div><a href="/">Животные</a></div>
<form action="/add" method="post">
<div>Животное: <input type="text" name="poroda"></div>
<div>Имя: <input type="text" name="name"></div>
<div>Возраст: <input type="text" name="age"></div>
<div><button type="submit">Добавить</button></div>
</form>
</body>
</html>
При нажатии на кнопку "Добавить" выкидывается вот что:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback. Mon Feb 19 20:08:52 MSK 2024 There was an unexpected error (type=Internal Server Error, status=500).
Что с этим делать?
Ответы (1 шт):
Автор решения: Software Selfaware
→ Ссылка
Я удалил запись, которую добавил вручную через SQL. То есть мне она мешала присвоить ID моей первой записи, добавленной через форму HTML. Проблема решена.