我正在尝试创建一个最小的 Spring Boot 应用程序,允许我使用 前端有 ReactJS。
我想要实现的目标
当我运行 Spring Boot 应用程序时,它应该显示一个带有 React 的页面 其内部的组件。
项目结构
您可以找到源代码here .
我得到了什么
当我
- 运行
mvn clean
mvn clean spring-boot:run` 和 - 导航到
http://localhost:8080
,
我收到 404 错误(见下文)。
如何修复它?
注释:
- 我复制了教程 React.js and Spring Data REST 中的方法.
- 别告诉我要摆脱 Kotlin,这不太可能是 Kotlin 导致了问题。
更新 1(2017 年 8 月 7 日 22:17 MSK):
可以找到mvn clean spring-boot:run
的输出here .
更新 2(2017 年 8 月 7 日 22:44 MSK):
将 MyApp
类中的 RestController
更改为 Controller
并移动 main
它里面的方法没有帮助。
目前,MyApp
类如下所示:
@SpringBootApplication
@Controller
open class MyApp {
fun main(args: Array<String>) {
SpringApplication.run(MyApp::class.java,*args)
}
}
最佳答案
现在我没有 Kotlin 经验,但我可以告诉您,您在以下代码中缺少一些内容(除了额外的 RestController
注释)。
@SpringBootApplication
@RestController
open class MyApp {
}
在 Java 中,此类除了通过运行以下静态方法在其中包含 main
之外,还实现了 ServletContextInitializer
:
SpringApplication.run(MyApp.class, args);
我希望这会有所帮助。
更新
尝试还添加一个模板解析 bean,现在它不是执行此操作的唯一方法,但值得一试,如下所示:
@Bean
public ClassLoaderTemplateResolver yourTemplateResolver() {
ClassLoaderTemplateResolver yourTemplateResolver = new ClassLoaderTemplateResolver();
yourTemplateResolver.setPrefix("templates/");
return yourTemplateResolver;
}
并将以下几行添加到您的 application.properties 中:
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
不要忘记将 thymeleaf 导入到您的 POM 中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
关于java - 为什么这个 Spring Boot 应用程序找不到主页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45554157/