我是 Spring Boot 新手。每当我运行 Spring Boot 应用程序时,都会出现错误。 需要帮助来运行我的 Spring Boot 应用程序。
错误信息: 白标错误页面
此应用程序没有/error 的显式映射,因此您将其视为后备。
2016 年 10 月 10 日星期一 10:39:54 WAT 出现意外错误(类型=未找到,状态=404)。 没有可用的消息
代码:
package hello;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";
}
}
package com.HelloWorld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}
- greeting.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>
最佳答案
问题是因为您的 Controller 与 Spring Boot 应用程序类的包结构不同,在这种情况下,需要将 @ComponentScan 添加到 Spring Boot 应用程序类中。
试试这个:
@ComponentScan(basePackages={"hello"})
关于java - 运行 Spring Boot 应用程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39957996/