巨坑,spring boot 打成war包部署到Tomcat
1)主要流程
环境:
spring boot 2.1.1.RELEASE
apache-tomcat-8.5.35
jdk 1.8.0_77
参考网上修改
1> pom.xml
将<packaging>jar</packaging> 改成 <packaging>war</packaging>
去除自带tomcat
<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支持servlet2.5,即支持tomcat6(供参考,未亲测)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-legacy</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
在编译时引入servlet
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
将jar包形式程序入口的Application启动类改造,@ServletComponentScan支持servlet使用,注解@Controller、@WebServlet可以正常使用
@Component
@ServletComponentScan
@SpringBootApplication
public class AlidemoApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AlidemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(AlidemoApplication.class, args);
}
}
打war包有两种形式:
第一种:idea菜单栏Build > Build Artifacts… > 工程名:war > Build
第二种:maven命令在项目名路径下输入 mvn clean package,如 D:\cjh\test_projects\jx_cloud\alidemo> mvn clean package
部署:将war包拷贝至tomcat的webapps,启动tomcat,访问如本地即 localhost:8080/war包名/{@RequestMapping配置的路径}
2)遇到的问题
2.1 重打war包时,如有其他文件在使用,比如SSH Xftp在传输占用,打包会报错失败。关掉占用的文件或工具即可。
2.2 war包部署到tomcat7,Controller无法访问到,servlet访问没问题。tomcat7启动报错如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultValidator' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Bean instantiati
on via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instan
tiate [org.springframework.validation.beanvalidation.LocalValidatorFactoryBean]: Factory method 'defaultValidator' threw
exception; nested exception is java.lang.NoClassDefFoundError: javax/el/ELManager at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to startcomponent [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/alidemo-0.0.1-SNAPSHOT]]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1022)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:994)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1127)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:2021)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
发现部署到Tomcat8没问题,原因还未查明。在此抛砖引玉,打个记号。
3)亲测有效
以上经过均测试,有问题随时交流。
最后,感谢大佬们!