Люди добрые помогите разобраться что я делаю не так у себя в коде, почему у меня на локалхосте не поднимается сайт пишет мне (This application has no explicit mapping for /error, so you are seeing this as a fallback.There was an unexpected error (type=Method Not Allowed, status=405).) Вот мой application.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/user_db
spring.datasource.username=mysql
spring.datasource.password=mysql
Вот мой класс модели
@Data
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
@Column(name = "age")
private int age;
@Column(name = "course")
private int course;
}
Вот интерфейс репозитория
public interface StudentRepository extends JpaRepository<Student, Long> {
}
Вот мой класс Сервис
@Service
public class StudentService {
@Autowired
public StudentRepository repository;
public Student findById(Long id){
return repository.findById(id).orElse(null);
}
public List<Student> findAll(){
return repository.findAll();
}
public Student saveStudent(Student student){
return repository.save(student);
}
public void deleteStudent(Long id){
repository.deleteById(id);
}
}
Вот мой контроллер
@Controller
public class StudentController {
@Autowired
public StudentService studentService;
@GetMapping("/students")
public String findAll(Model model){
List<Student> allStudent = studentService.findAll();
model.addAttribute("students", allStudent);
return "student-list";
}
@GetMapping("/student-create")
public String createStudentForm(Student student){
return "student-create";
}
@PostMapping
public String createStudent(Student student){
studentService.saveStudent(student);
return "redirect:/students";
}
}
И вот мой html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Student-List</title>
</head>
<body>
<div th:switch="${students}">
<h2 th:case="null">Студентов не найдено!</h2>
<div th:case="*">
<h2>Студенты</h2>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Имя</th>
<th>Фамилия</th>
<th>Возраст</th>
<th>Курс</th>
</tr>
</thead>
<tbody>
<tr th:each="student : ${students}">
<td th:text="${student.id}"> </td>
<td th:text="${student.name}"> </td>
<td th:text="${student.sername}"> </td>
<td th:text="${student.age}"> </td>
<td th:text="${student.course}"> </td>
<td th:href="@{student-update/{id}(id=${student.id})}">Редактировать </td>
<td th:href="@{student-delete/{id}(id=${student.id})}">Удалить </td>
</tr>
...
</tbody>
</table>
</div>
<p><a href="/student-create">Добавить нового пользователя</a></p>
</div>
</body>
</html>
В базе через phpmyadmin создал бд user_db и там таблицу students и там уже добавил пару студентов что бы хоть что-то вывелось.
Вот что в логах вывело
:: Spring Boot :: (v2.4.5)
2021-04-22 10:23:27.785 INFO 4528 --- [ main] c.e.studentlist.StudentlistApplication :
Starting StudentlistApplication using Java 1.8.0_201 on DESKTOP-7BUHOLO with PID 4528
(C:\projectIdea\studentlist\target\classes started by Denis in C:\projectIdea\studentlist)
2021-04-22 10:23:27.789 INFO 4528 --- [ main] c.e.studentlist.StudentlistApplication :
No active profile set, falling back to default profiles: default
2021-04-22 10:23:29.207 INFO 4528 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate :
Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-04-22 10:23:29.297 INFO 4528 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate :
Finished Spring Data repository scanning in 71 ms. Found 1 JPA repository interfaces.
2021-04-22 10:23:29.947 INFO 4528 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer :
Tomcat initialized with port(s): 8080 (http)
2021-04-22 10:23:29.957 INFO 4528 --- [ main] o.apache.catalina.core.StandardService :
Starting service [Tomcat]
2021-04-22 10:23:29.957 INFO 4528 --- [ main] org.apache.catalina.core.StandardEngine :
Starting Servlet engine: [Apache Tomcat/9.0.45]
2021-04-22 10:23:30.105 INFO 4528 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] :
Initializing Spring embedded WebApplicationContext
2021-04-22 10:23:30.105 INFO 4528 --- [ main] w.s.c.ServletWebServerApplicationContext :
Root WebApplicationContext: initialization completed in 2233 ms
2021-04-22 10:23:30.365 INFO 4528 --- [ main] o.hibernate.jpa.internal.util.LogHelper :
HHH000204: Processing PersistenceUnitInfo [name: default]
2021-04-22 10:23:30.418 INFO 4528 --- [ main] org.hibernate.Version :
HHH000412: Hibernate ORM core version 5.4.30.Final
2021-04-22 10:23:30.533 INFO 4528 --- [ main] o.hibernate.annotations.common.Version :
HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-04-22 10:23:30.630 INFO 4528 --- [ main] com.zaxxer.hikari.HikariDataSource :
HikariPool-1 - Starting...
2021-04-22 10:23:30.880 INFO 4528 --- [ main] com.zaxxer.hikari.HikariDataSource :
HikariPool-1 - Start completed.
2021-04-22 10:23:30.897 INFO 4528 --- [ main] org.hibernate.dialect.Dialect :
HHH000400: Using dialect: org.hibernate.dialect.MySQL55Dialect
2021-04-22 10:23:31.859 INFO 4528 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator :
HHH000490: Using JtaPlatform implementation:
[org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-04-22 10:23:31.868 INFO 4528 --- [ main] j.LocalContainerEntityManagerFactoryBean :
Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-04-22 10:23:32.172 WARN 4528 --- [ main] JpaBaseConfiguration$JpaWebConfiguration :
spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during
view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-04-22 10:23:32.321 INFO 4528 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor :
Initializing ExecutorService 'applicationTaskExecutor'
2021-04-22 10:23:32.594 INFO 4528 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer :
Tomcat started on port(s): 8080 (http) with context path ''
2021-04-22 10:23:32.604 INFO 4528 --- [ main] c.e.studentlist.StudentlistApplication :
Started StudentlistApplication in 5.643 seconds (JVM running for 6.948)
2021-04-22 10:23:52.616 INFO 4528 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] :
Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-04-22 10:23:52.616 INFO 4528 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet :
Initializing Servlet 'dispatcherServlet'
2021-04-22 10:23:52.618 INFO 4528 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet :
Completed initialization in 2 ms
2021-04-22 10:23:52.635 WARN 4528 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not
supported]
2021-04-22 10:23:54.655 WARN 4528 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not
supported]