摘要 spring-boot整合web爲啥能有這麼多問題,好幾天都是崩潰的。本文我就一個問題一個問題的交代,而且給出個人解決方案。解決方案未必是通用的,我仍是建議你們邊跟代碼邊猜想,找到解決途徑。最坑的就是百度那麼多爛文章,耗費時間不解決問題。html
問題1: Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 不能正常跳轉到jsp文件.java
問題定位:git
我走了下Dispatcher、AbstractView、JstlView這三個類。發現有一個參數一直是異常的:_contextHandlergithub
若是正常它應該是咱們項目信息:web
o.m.j.p.JettyWebAppContext{/es-web,file:/D:/材料/源碼/zhangkaitao/123/es-master/web/src/main/webapp/},file:/D://es-master/web/src/main/webapp/spring
而在個人項目裏,它是這個數據:springboot
o.s.b.c.e.j.JettyEmbeddedWebAppContext@527d5e48{/,file:///C:/Users/user/AppData/Local/Temp/jetty-docbase.4093955487572732783.8080/,AVAILABLE} 很顯然,這個數據應該是配合錯誤的,這個類JettyEmbeddedWebAppContext是springboot裏面的。打開它的父類,發現本身真的是太淺薄了,原來這個類也是能夠配置不少信息的。這個參數太底層了,咱們沒法直接操做,那麼jetty給了咱們的配置工廠是AbstractEmbeddedServletContainerFactory,經過它可以解決。 想了半天如何添加,也疑惑爲何項目根目錄是這個c:文件夾,後來我將MyApplication裏面的pplication.sources(MyApplication.class) 改爲pplication.sources(this.getClass());就好使了。這一點必定跟加載機制有關係,深刻我不清楚。app
問題解決:webapp
@SpringBootApplication(exclude = PersistenceExceptionTranslationAutoConfiguration.class) @EnableConfigurationProperties @ComponentScan("com.framework.demo") public class MyApplication extends SpringBootServletInitializer { protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(this.getClass()); } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
問題2:不要直接的運行MyApplication文件。 雖然網上一大堆直接運行MyApplication可以順利的訪問到項目資源,但是我直接運行確實找不到jsp頁面的。因此這裏建議用spring-boot的插件運行(mvn的spring-boot:run啓動)。jsp
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.framework.demo.web.MyApplication</mainClass> <addResources>true</addResources> <layout>ZIP</layout> <classifier>exec</classifier> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
問題3:mvn install的時候測試類、文檔類會出現問題
這個問題雖然最好找到解決方案,可是若是你沒有時間,很着急,那麼使用如下命令暫時繞過也是不過的選擇。
install -D maven.javadoc.skip=true -D maven.test.skip=true
問題4:打包出錯 repackage failed: Source must refer to an existing file sprinboot的打包是二次打包,必須是在已有包的基礎上纔可以打包成功(war、jar)
spring-boot:run命令須要有正確的依賴關係才能啓動,若是使用spring-boot打包確定會找不到model之間的依賴關係。因此配置要改下。
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.framework.demo.web.MyApplication</mainClass> <addResources>true</addResources> <layout>ZIP</layout> //把原來的包install到本地私服中,若是不配置放入私服中的是springboot形式的包,結構徹底不一樣,不一樣model之間的依賴關係將被破壞。 <classifier>exec</classifier> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
問題五、spring-boot打包
spring-boot:repackage是二次打包命令,須要咱們事先打包好war。
spring-boot 之因此可以在程序打完包以後直接就可以運行,是由於它是將咱們項目包包裹在了本身的運行包中,進行了二次打包,深刻的有待接下來的研究。下面給出maven 的war包打包插件,注意下,若是項目中沒有了web.xml,必定要將failOnMissingWebXml加上。
<plugin> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> <webappDirectory>${project.build.directory}/${module.war.name}.war</webappDirectory> <warSourceDirectory>src/main/webapp</warSourceDirectory> <webResources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <filtering>true</filtering> <targetPath>WEB-INF/classes/META-INF</targetPath> </resource> <resource> <directory>src/main/webapp/WEB-INF</directory> <filtering>true</filtering> <targetPath>WEB-INF</targetPath> <includes> <include>log4j.xml</include> </includes> </resource> </webResources> <resourceEncoding>UTF-8</resourceEncoding> </configuration> <executions> <execution> <id>default-war</id> <phase>package</phase> <goals> <goal>war</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.framework.demo.web.MyApplication</mainClass> <addResources>true</addResources> <layout>ZIP</layout> <classifier>exec</classifier> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
問題:靜態文件訪問不到
網上好多文章有點兒忽悠人的成分,能不能說清楚類路徑先,而後在說spring-boot下面的默認資源路徑在哪裏放置,害得我看到頁面正常訪問的那一刻心裏是崩潰的。spring-boot有五個默認的靜態資源放置路徑:
一、locations=[ServletContext resource [/],
二、class path resource [META-INF/resources/],
三、class path resource [resources/],
四、class path resource [static/],
五、class path resource [public/]
請各位注意,這塊和咱們平時配置是不一樣的。這裏面說的是類路徑,也就是說咱們平時java文件編譯成class文件那個目錄下。而webapp下面springboot是不會去找的。另外,一些文字說這樣添加路徑,classpath:WEB-INF/,這種提示很容易讓人誤解是和咱們以前的配置習慣一致,貽害不淺!!配置的時候必定要記得是類路徑,不是咱們以前的webapp路徑下!
歡迎來個人公衆號:hyssop的後花園
參考文獻 https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples http://docs.spring.io/spring-boot/docs/current/maven-plugin/index.html