问题描述:
新创建一个 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-web</artifactId>
</dependencies>