【问题标题】:HTTP Status 404 – Not Found Spring BootHTTP 状态 404 - 未找到 Spring Boot
【发布时间】:2020-09-19 08:04:15
【问题描述】:

我正在使用 spring boot 创建简单的 crud 系统。当我加载页面时,它正在正常工作。当我添加一个新链接时,它显示为 HTTP 状态 404 – 未找到 Spring Boot 我不明白为什么我所做的一切都是正确的,我在下面的代码中附上了屏幕截图。

错误屏幕

员工控制器

@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>

文件夹结构

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

【问题讨论】:

  • 我在上面添加了 pom.xml。如何添加这个 spring-boot-starter-thymeleaf
  • 我已经添加了请检查上面的 pom.xml
  • application.properties 我在上面添加,请检查如何添加模板
  • 您的Employee.java 文件看起来如何?工作部分(我猜这是/ 端点)正在返回一个EmployeeService,您很可能在其中调用Thymeleaf 来显示index.html。如果 Employee 类不是服务,而是模型,这当然不会起作用。 编辑:文件夹结构截图只列出student文件,Employee文件在哪里?有什么要开始的吗?

标签: spring spring-boot


【解决方案1】:

问题是包结构,你没有遵循spring boot和spring框架的标准包结构。默认情况下,spring boot 应用程序只会扫描使用主类及其子包中的任何构造型注释的类。我建议将所有依赖类移动到主类的子包下

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

【讨论】: