0

So I've set up a first time test application Using Spring-Boot, Kotlin, and Intellij.

When I run the project everything seems to start up fine in the console. But when I navigate to http://localhost:8080/ in my browser it gives me a 404 error and a page that looks like this:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this 
as a fallback.

Wed Aug 29 07:54:39 MDT 2018
There was an unexpected error (type=Not Found, status=404).
No message available

Why is it not navigating to the page I created?

Main Class:

package com.daniel.anderson.demo
import ... 


@SpringBootApplication
class DemoApplication

    fun main(args: Array<String>) {
        runApplication<DemoApplication>(*args)
    }

The Controller code:

package com.daniel.anderson.demo.controlers
import ... 

@Controller
class HtmlController {

    @GetMapping("/")
    fun blog(model: Model) : String {
        model.addAttribute("title","Blog")
        return "blog"
    }    
}

Mustach Files: blog.mustache

{{> header}}

<h1>{{title}}</h1>
<p>hello world</p>

{{> footer}}

footer.mustache

</body>
</html>

header.mustache

<html>
<head>
  <title>{{title}}</title>
</head>
<body>

Btw, I get the same error in postman.

Dan Anderson
  • 1,062
  • 13
  • 26

3 Answers3

1

The issue was the @Controller in my controller If I changed the annotation to @RestController then it worked. My understanding is that I should be able to use the @Controller annotation but it appears you need the @RestController

So I found some really use full info here Spring boot error 404

Dan Anderson
  • 1,062
  • 13
  • 26
1

I think the problem is the lack of the annotation @ResponseBody on the method

@GetMapping("/")
@ResponseBody
fun demo(model: Model): String {
    model["title"] = "Demo"
    return "Hello world?!"
}

This must work

JoseLuis
  • 151
  • 2
  • 9
1

I know this is an old thread and there is also an answer from the creator himself, but this behavior is typical if you have not added the mustache dependency.

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

I'm not 100% sure but if you change the annotation from @Controller to @RestController, the method returns only the string "blog".

Philipp W
  • 11
  • 1