Spring 引导:无法访问本地主机上的页面 html (404)

Spring Boot: Cannot access page html on localhost (404)

我正在尝试调整 Spring Boot 网站上的控制器示例。不幸的是我有 错误 404 当我试图访问 localhost:8080/NewFile URL

我的 EcomercController.java :

package com.techprimers.jpa.springjpahibernateexample.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class EcomercController {
    @RequestMapping(value = "/NewFile", method = RequestMethod.GET)
    public String NewFile(){
         return "NewFile";
    }
}

而且我在 porm.xml

中添加了这个依赖项
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

和我的路径包

com.exemple

----- com.exemple.web.EcomercController.java

com.exemple.SpringJpaHibernateExampleApplication

我的文件 NewFile.html 在

resources->static->NewFile.html

您的 html 文件应该在 templates 文件夹中

resources->templates->NewFile.html

阅读 Spring Boot Serving static content 以了解资源映射。

将您的 Html 文件移动到 resources->templates 文件夹

将您的 html 文件放在其中一个中-

src/main/resources/resources/index.html
src/main/resources/static/index.html 
src/main/resources/public/index.html

既然你选择了这个-

src/main/resources/resources/index.html

然后放入模板中。

确保在应用程序配置中(在资源文件夹下)您没有上下文映射。我认为默认值是“/api”,在这种情况下,您需要访问 URL 作为:

本地主机:8080/api/NewFile

建议:不要计算api您的 URI。请改用新文件或新文件。