亲宝软件园·资讯

展开

SpringBoot找不到javax.servlet.Filter

eknown 人气:0

SpringBoot找不到javax.servlet.Filter的问题

新创建一个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>

启动SpringBoot项目找不到报错

java.lang.ClassNotFoundException: javax.servlet.Filter

出现该问题是因为缺少javax.servlet.Filter的Tomcat-embed-core-xxx.jar包;我们通常使用 spring-boot-starter-web 注入即可。

但是我检查了maven本地仓库中已经有,且项目依赖maven中也存在该包;pom文件也注入了spring-boot-starter-web;编译也通过不报错。但是就是项目启动报错。

经过一番折腾,我仔细对比了pom文件的配置项内容,发现有细小差异;

这是我配置的(有问题的)

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
</dependency>

正确的配置(在远程maven仓库中搜索的配置)

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>2.3.1.RELEASE</version>
 </dependency>

经过对比,发现 <scope>provided</scope> 是导致报错的根源;所以直接删除这个属性;添加版本号属性,重启错误得到解决。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论