剛學習SpringBoot,第一步確定是搭建一個helloWorld的程序.git
在集成SpringMVC的過程,發現一個異常詭異的問題.寫了2個controller.github
@RestController public class MyRestController { @RequestMapping("/") String home() { return "hello"; } }
@Controller public class MyViewController { @GetMapping("/hello") public String index(Map<String, Object> model){ model.put("time", new Date()); return "hello"; } }
MyRestController正常運行, MyViewController不管如何都報404. 網上查詢答案,反覆確認已經作了各類修改(打包jar該war;引入tomcat\jsp依賴;修改webapp路徑)仍然無效.web
一直搜索關鍵字"SpringBoot Jsp 404",沒法得到突破. 後面偶然發現一個網友的啓動步驟是spring
mvn:spring-boot:run. 嘗試一下終於成功.apache
老是得到突破,修改關鍵字"mvn spring-boot:run main() 區別"找到如下這個文章,segmentfault
http://www.javashuo.com/article/p-fevzxhth-dd.html .裏面提供的解決方法"(去掉將pom.xml中tomcat-embed-jasper依賴<scope>provided</scope>)"在我這裏仍不奇效. 轉戰StackOverflow,tomcat
搜索新的關鍵字,找到這個文章app
https://stackoverflow.com/questions/30237768/run-spring-boots-main-using-ide 裏面的人和我遇到一樣的問題"Seems like mvn spring-boot:run
does some more magic that does not happen when running the main
directly."webapp
相關答案帖子裏的1L,2L說的很詳細了. IntelliJ IDEA沒有將<scope>provided
</scope>的依賴注入到類路徑中,用main()方法啓動的話,pom.xml裏添加的這個jsp
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
不會被加載 .用mvn spring-boot:run指令就能夠了.
另分享個今天的收穫:
https://github.com/spring-projects 發現這個神奇的github帳號,Spring官方維護的全部模塊的demo,哪裏不會點哪裏,so easy!