javaspring-bootthymeleafspring-thymeleaf

Why am I getting a 404 Not Found error in Thymeleaf Using SpringBoot


I have a controller that is using the RequestMapping annotation but whenever I visit the /books page I get a white label error.

package come.saeedado.controllers;

import come.saeedado.BookDemo.repositories.BookRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class BookController {

    private final BookRepository bookRepository;

    public BookController(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    @RequestMapping("/books")
    public String getBooks(Model model){

        model.addAttribute("books", bookRepository.findAll());

        return "books/list";
    }
}

My HMTL is as follows

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Spring Framework Guru</title>
</head>
<body>
<h1>Book List</h1>

<table>
    <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Publisher</th>
    </tr>
    <tr th:each="book : ${books}">
        <td th:text="${book.id}">123</td>
        <td th:text="${book.title}"> Spring in Action</td>
        <td th:text="${book.publisher.name}">Wrox</td>
    </tr>
</table>

</body>
</html>

This is my current project structure Image of Project Structure

I was expecting a list of Books to be displayed


Solution

  • The BookDemoApplication file location is incorrect, it should be outside the controller layer.At this point, the controller layer has not been detected