[英]Why can't this Spring Boot application find the main page?
我正在嘗試創建一個最小的Spring Boot應用程序,該應用程序允許我在前端上使用ReactJS。
我想要達到的目標
當我運行Spring Boot應用程序時,它應該顯示一個帶有React組件的頁面。
項目結構
您可以在此處找到源代碼。
我得到什么
當我
mvn clean
mvn clean spring-boot:run`和 http://localhost:8080
, 我收到404錯誤(請參閱下文)。
我該如何解決?
筆記:
更新1(07.08.2017 22:17 MSK):
mvn clean spring-boot:run
的輸出可以在這里找到。
更新2(07.08.2017 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
不要忘記將百里香葉導入您的POM:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.