Home > Net >  Spring Boot ThymeLeaf Not Loading HTML
Spring Boot ThymeLeaf Not Loading HTML

Time:04-18

I have a Java and Maven project in which I have the following controller (src/main/java/com/example/romannumerals/controller/RomanNumeralController):

@RestController
public class RomanNumeralController {
    @GetMapping("/roman-numerals/{number}")
    public String RomanNumeral(@PathVariable int number){
        return RomanNumeralsService.numberToNumeral(number);
    }

    @GetMapping("/roman-numerals")
    public String RomanNumeral() {
        return "roman-numeral";
    }
}

I some HTML (src/main/resources/templates/roman-numeral.html) that looks like this:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>From Numbers to Roman Numerals</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Roman Numerals
</body>
</html>

I expected this HTML to be displayed when I called the method on the controller that has no parameter. Instead, I get the string "roman-numeral" printed to the screen. Am I doing something wrong? I thought Thymeleaf would recognise that I want to display the HTML page with that name instead of the string being displayed.

CodePudding user response:

Replace @RestController with @Controller. @RestController will make the returned object the HTTP response body.

  • Related