Spring 启动应用程序显示 index.html 的 404

nao*_*oru 3 spring-boot

我有一个 Spring 启动应用程序和一个位于 src/main/resources/static 中的静态 index.html 文件

我的控制器很直接,看起来像这样

@Controller
@Log4j
public class StartController {

    @Autowired
    OwnerRepository ownerRepository;

    @RequestMapping("/")
    public String index() {
        return "index";


    }

}
Run Code Online (Sandbox Code Playgroud)

我唯一的配置是主要配置,当我点击 URL 时,它只有 @SpringBootApplication 我得到 404,根据我的理解,在启动时的日志中不需要进一步的配置,我看到以下内容

请求 [/index] 的 URI 模板变量为 {}

这是我的 POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.datasol</groupId>
    <artifactId>alquifacil</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>AlquiFacil</name>
    <description>spring boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <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-data-mongodb</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
Run Code Online (Sandbox Code Playgroud)

小智 7

On your pom.xml i didn't see any template engine like Thymeleaf, Velocity ecc but you have spring-boot-starter-web, so basically you resource folder its configurated, but you should put your file index.html on templates folder. Static folder serve to contain your asset files.

如果您在本地工作,请尝试: localhost:8080/localhost:8080/alquifacil/假设您使用端口 8080 或在 pom.xml 中添加模板引擎依赖项,如 Thymeleaf

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)