【问题标题】:Why am I getting a 404 error when I go to localhost:8080/ using SpringBoot为什么我使用 SpringBoot 访问 localhost:8080/ 时出现 404 错误
【发布时间】:2019-02-04 09:11:50
【问题描述】:

所以我使用 Spring-Boot、Kotlin 和 Intellij 设置了第一次测试应用程序。

当我运行项目时,控制台中的一切似乎都正常启动。但是,当我在浏览器中导航到 http://localhost:8080/ 时,它给了我一个 404 错误和一个看起来像这样的页面:

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

为什么没有导航到我创建的页面?

主类:

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


@SpringBootApplication
class DemoApplication

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

控制器代码:

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

@Controller
class HtmlController {

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

胡子文件: blog.mustache

{{> header}}

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

{{> footer}}

footer.mustache

</body>
</html>

header.mustache

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

顺便说一句,我在邮递员中遇到了同样的错误。

【问题讨论】:

    标签: spring spring-boot kotlin


    【解决方案1】:

    问题是我的控制器中的@Controller 如果我将注释更改为@RestController,那么它就起作用了。我的理解是我应该能够使用@Controller 注释,但看来您需要@RestController

    所以我在这里找到了一些真正有用的完整信息Spring boot error 404

    【讨论】:

      【解决方案2】:

      我认为问题在于方法上缺少@ResponseBody 注释

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

      这必须有效

      【讨论】:

        【解决方案3】:

        我知道这是一个旧线程,并且创建者自己也有一个答案,但是如果您没有添加 mustache 依赖项,这种行为是典型的。

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

        我不是 100% 确定,但是如果您将注解从 @Controller 更改为 @RestController,则该方法仅返回字符串“blog”。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-02-09
          • 2014-03-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-02
          • 2021-11-12
          • 1970-01-01
          相关资源
          最近更新 更多