spring boot由于内嵌tomcat容器,因此能够经过打包为jar包的方法将项目发布,可是如何将spring boot项目打包成可发布到tomcat中的war包项目呢?html
1. 既然须要打包成war包项目,首先须要在pom.xml文件中修改打包类型,将spring boot默认的<packaging>jar</packaging>修改成<packaging>war</packaging>形式;java
2. 其次spring boot的web项目中内嵌tomcat服务器,因此若是咱们想要发布war包到tomcat项目,要讲spring boot中内嵌的tomcat包依赖排除,否则产生冲突,打开下面代码中的注释便可。git
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> --> </dependency>
有一点想说的是,若是本地开发的时候依然想要使用spring boot内嵌tomcat进行调试,添加以下依赖便可;github
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
3. spring boot发布jar包web程序的入口是main函数所在的类,使用@SpringBootApplication注解。可是若是war包发布至tomcat,须要增长 SpringBootServletInitializer
子类,并覆盖它的 configure
方法,或者直接将main函数所在的类继承 SpringBootServletInitializer
子类,并覆盖它的 configure
方法。代码举例以下,web
@SpringBootApplication public class DemoApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
以上就完成了spring boot项目打包war包的全部步骤,能够发布至tomcat7及其以上版本。spring
可是以上流程改造完spring boot打包war包发布至tomcat6版本以后,浏览器访问项目地址会给出404的错误?为何呢,一头雾水,通过我一番查阅资料以及实验,得出如下结论,浏览器
首先spring boot支持的servlet容器以下,能够看出spring boot最低支持的servlet版本是3.0,可是tomcat6的servlet版本是2.5,这样的话上面的流程是没法支持tomcat6发布spring boot项目的,tomcat
可是又google了一番,发现已经有人在解决这个问题了,https://github.com/dsyer/spring-boot-legacy,springboot
标题就代表了它是为了让spring boot支持servlet2.5,恰好解决咱们的痛点,使用步骤以下:服务器
1. pom.xml中添加spring-boot-legacy的依赖,
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-legacy</artifactId> <version>1.1.0.RELEASE</version> </dependency>
2.手动替换web.xml文件。可是在发布war包中发现metricFilter提示空指针异常,我就简单粗暴的将filter过滤了,注释以下。 所要替换的web.xml文件的未知以下 : {工程目录}/src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>com.example.DemoApplication</param-value> </context-param> <listener> <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class> </listener> <!-- <filter> <filter-name>metricFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>metricFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextAttribute</param-name> <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
完成以上两个步骤就可让spring boot项目支持tomcat6的部署了,解决。
思考:spring boot封装带来了便利性,同时也带来了出问题的解决复杂度,若是不了解原始的spring web开发模式,出现问题很难解决。即便我如今解决了支持tomcat6的spring boot支持问题,可是不太明白解决的原理,filter出现空指针是为何?因此深刻一些原理性的东西,学习技术的基础性的东西很是重要。你们能够明白的能够解释解释2.5支持的原理,以及filter空指针的异常缘由。
最后感谢前辈们的资料,
参考资料: