Search code examples
spring-bootthymeleaf

How to debug a 404 error when using Spring Boot and Thymeleaf?


Here is the Controller class:

    package me.ankit.zooplus;

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

    @Controller
    public class HomeController {   
        @RequestMapping("/home")
        public String home() {

        return "home";
    }
}

and this is my application class

package me.ankit.zooplus;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ZooplusApplication  {

public static void main(String[] args) {
    SpringApplication.run(ZooplusApplication.class, args);
 }
}

My folder structure is as follows: file structure

I have this from maven [copy and pasted few lines from the console]:

INFO 7484 --- [lication.main()] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/home]}" onto public java.lang.String me.ankit.zooplus.HomeController.home()
INFO 7484 --- [lication.main()] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
INFO 7484 --- [lication.main()] me.ankit.zooplus.ZooplusApplication      : Started ZooplusApplication in 10.554 seconds (JVM running for 16.948)

[INFO] BUILD SUCCESS

and Finally my pom.xml

<?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>me.ankit</groupId>
<artifactId>zooplus</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>zooplus</name>
<description>zooplus: Currency Converter</description>

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

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <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-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <!-- hot swapping, disable cache for template, enable live reload -->       
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

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

What could have been gone wrong ? Also is there a place/view/log, I can go in future to check what is wrong ?


Solution

  • I have cloned your project and ran it. I do have couple of notes:

    1. In your pom.xml, you have added spring-boot-starter-security dependency, which will create a default login page once you access your web application. If you don't need security at this stage remove all security dependencies from your pom.xml or you have to provide Security Configuration for your application.
    2. When accessing your application, make sure you add "home" to your URL: http://localhost:8080/home because this is mapped to your controller. If you want to add a default context to your application add the following to application.properties: server.servlet.contextPath=/zooplus, then you can access it through the URL: http://localhost:8080/zooplus/home