tutusfun
tutusfun

Reputation: 39

HTTP Status 404 – Not Found Spring Boot

I am creating simple crud system using spring boot. When I load the page it is working currectly. when I add a new link it display as HTTP Status 404 – Not Found Spring Boot I don't why everything I made currect way I attached screenshot with code below.

Error Screen enter image description here

EmployeeController

@Autowired
private EmployeeService service;

@RequestMapping("/")
public String viewHomePage(Model model) {
    List<Employee> listemployee = service.listAll();
    model.addAttribute("listemployee", listemployee);

    return "index";
}

@RequestMapping(value = "/new")
public String add(Model model) {
    model.addAttribute("employee", new Employee());
    return "new";
}

index.html

<table class="table">
<tbody>
<tr>
    <td>
        <a th:href="@{'/new'}">Add new</a>
    </td>
</tr>
<tr  th:each="employee : ${listemployee}">
    <td th:text="${employee.id}">Employee ID</td>
    <td th:text="${employee.firstName}">FirstName</td>
    <td th:text="${employee.lastName}">LastName</td>

    <td>
        <a th:href="@{'/edit/' + ${employee.id}}">Edit</a>
        &nbsp;&nbsp;&nbsp;
        <a th:href="@{'/delete/' + ${employee.id}}">Delete</a>
    </td>
</tr>
</tbody>
</table>

folder structure

enter image description here

porm.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

application.properties

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/lindaschool?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
server.error.whitelabel.enabled=false
server.port=7020
spring.datasource.username=root
spring.datasource.password=
#logging.level.root=WARN
spring.jpa.open-in-view=false
spring.thymeleaf.cache=false

Upvotes: 1

Views: 1423

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40078

The problem is package structure, you not following the standard package structure of spring boot and spring framework. By default spring boot application will only scans the classes annotated with any stereotype from main class and it's sub packages. I would recommend to move all dependency classes under sub packages of main class

com.example.crudbank
  |
  -------------------->CrudBankApplication.java
com.example.crudbank.service
  |
  --------------------->StudentService
com.example.crudbank.controller
  |
  ----------------------> StudentController.java
com.example.crudbank.repository
  |
  -----------------------> StudentRepository.java
com.example.crudbank.domain
  |
  ------------------------>StudentDomain.java

Upvotes: 2

Related Questions