新创建一个SpringBoot项目,编译时出现了找不到javax.servlet.Filter的异常。

这个类位于tomcat-embed这个jar下面,这里的解决方法并不是像网上大部分所说的手动添加这个jar,因为这是由于没有添加spring-boot-starter-web依赖造成的。
springboot项目默认会添加spring-boot-starter和spring-boot-starter-test两个依赖,而web项目需要spring-boot-starter-web依赖。
下面对比一下spring-boot-starter和spring-boot-starter-web这两个依赖的区别:
spring-boot-starter-web:

spring-boot-starter,没有web:

所以在maven里添加下面这个依赖即可:
<dependencies>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-we</artifactId>
</dependencies>