我正在尝试在Spring Boot网站上改编Controller示例。不幸的是我有错误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文件移动到资源->模板文件夹中
将您的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
然后放在模板内。
请确保在applicaton配置(在resources文件夹下)中没有上下文映射。我认为默认值为“ / api”,在这种情况下,您需要以以下方式访问URL:
localhost:8080 / api / NewFile
推荐:不要大写URI。请改用新文件或新文件。