Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I am getting error 404 with Spring-boot and thymeleaf on an existing source?

I have a simple web application written on java 8 using spring boot and thymeleaf with the following dependencies:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>serving-web-content-initial</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>serving-web-content-initial</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>

with just one controller :

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String greeting(@RequestParam(name = "name", required = false, defaultValue = "World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }

}

under the resource folder of the same module (resources/templates/greeting.html) i have a sample html :

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'"/>
</body>
</html>

application properties is basically empty containing only the following line: logging.level.org.springframework.web=DEBUG

the request on URL http://localhost:8080/greeting is always returning error 404 no matter if accessed with http or https and without difference of the browser used.

Am I missing a part of the configuration or is it wrong? What can be the cause of this? Can it not be related with my project and be caused by some other configuration on my computer? Any suggestions will be appreciated great.

P.S. I have downgraded from version 2.5.2 of spring-boot-starter-parent to its lower version 2.4.10 and the problem is fixed.

like image 525
Илиян Д. Avatar asked Sep 13 '25 13:09

Илиян Д.


1 Answers

Just add this in application.properties:

spring.thymeleaf.prefix= classpath:/templates/
spring.thymeleaf.suffix= .html
like image 110
Faheem azaz Bhanej Avatar answered Sep 15 '25 02:09

Faheem azaz Bhanej