在IntelliJ IDEA里建了个Maven项目(用的spring boot)创建项目时操作跟之前在eclipse操作步骤基本差不多;
IDEA配置的spring boot项目的tomcat依赖为
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
其中<scope>provided</scope>的含义在另一篇文章里有详细说明。
启动时报错java.lang.ClassNotFoundException: javax.servlet.ServletContext。
后面试了一下,把范围标识 provided 改成 compile ,发现就可以运行了,所以就是 Run Application时,IDEA未加载 provided 范围的依赖包,导致启动时报错(eclipse里面好像默认会加载,所以在那边是能正确运行的)
解决方式 Run ->Edit Configurations
将选项Include dependencies with “Provided” scope勾上。
参考说明:For the Spring Boot applications, the Include dependencies with “Provided” scope option is enabled by default. (按理说对于Spring-Boot项目默认是启用的,我这默认没启用所以报错)