naoru
naoru

Reputation: 2227

Spring boot app showing 404 for index.html

I have a Spring boot application and a static index.html file located in src/main/resources/static

my controller is straight forward and looks like this

@Controller
@Log4j
public class StartController {

    @Autowired
    OwnerRepository ownerRepository;

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


    }

}

my only configuration is the main one and it only has the @SpringBootApplication Im getting 404 when I hit the URL, as per my understanding no further configuration is needed by the way in the logs on the startup im seeing the following

URI Template variables for request [/index] are {}

here is my 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>

Upvotes: 4

Views: 12453

Answers (3)

Mafei
Mafei

Reputation: 3801

please delete the target file and run again. it can be a cashing problem.

Upvotes: 1

Tonney Bing
Tonney Bing

Reputation: 230

html is a static resource so you can access by "localhost:8080/index.html" or use "forward" keyword

@RequestMapping("/")
public String index() {
   //has to be without blank spaces
   return "forward:index.html";
}

Upvotes: 3

user4713051
user4713051

Reputation:

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.

If you are working local, try: localhost:8080/ or localhost:8080/alquifacil/ assuming you are using port 8080 or add a template engine dependency in pom.xml like Thymeleaf

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Upvotes: 8

Related Questions