Spring Boot java.lang.NoClassDefFoundError: javax/servlet/Filter

前端 未结 8 1022
慢半拍i
慢半拍i 2020-12-08 01:44

I Started a new project with Spring Boot 1.2.3. I\'m getting error

java.lang.NoClassDefFoundError: javax/servlet/Filter

Gradle Dependencie

相关标签:
8条回答
  • 2020-12-08 02:16

    For Jar

    Add pom.xml

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
    0 讨论(0)
  • 2020-12-08 02:21

    Add the following dependency. The scope should be compile then it will work.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>compile</scope> 
    </dependency>
    
    0 讨论(0)
  • 2020-12-08 02:26
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    

    This should be

    compile("org.springframework.boot:spring-boot-starter-tomcat")
    
    0 讨论(0)
  • 2020-12-08 02:30

    It's not good to change the scope of your application dependencies. Putting the dependency as compile, will provide the dependency also in your artifact that will be installed somewere. The best think to do is configure the RUN configuration of your sping boot application by specifying as stated in documentation :

    "Include dependencies with 'Provided' scope" "Enable this option to add dependencies with the Provided scope to the runtime classpath."

    0 讨论(0)
  • 2020-12-08 02:38

    It's interesting things with IDE (IntelliJ in this case):

    • if you leave default, i.e. don't declare spring-boot-starter-tomcat as provided, a spring-boot-maven-plugin (SBMP) put tomcat's jars to your war -> and you'll probably get errors deploying this war to container (there could be a versions conflict)

    • else you'll get classpath with no compile dependency on tomcat-embed (SBMP will build executable war/jar with provided deps included anyway)

      • intelliJ honestly doesn't see provided deps at runtime (they are not in classpath) when you run its Spring Boot run configuration.
      • and with no tomcat-embed you can't run Spring-Boot with embedded servlet container.

    There is some tricky workaround: put Tomcat's jars to classpath of your idea-module via UI: File->Project Structure->(Libraries or Modules/Dependencies tab) .

    • tomcat-embed-core
    • tomcat-embed-el
    • tomcat-embed-websocket
    • tomcat-embed-logging-juli

    Better solution for maven case

    Instead of adding module dependencies in Idea, it is better to declare maven profile with compile scope of spring-boot-starter-tomcat library.

    <profiles>
        <profile>
            <id>embed-tomcat</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <scope>compile</scope>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
    

    while spring-boot-starter-tomcat was declared provided in <dependencies/>, making this profile active in IDE or CLI (mvn -Pembed-tomcat ...) allow you to launch build with embedded tomcat.

    0 讨论(0)
  • 2020-12-08 02:39

    That looks like you tried to add the libraries servlet.jar or servlet-api.jar into your project /lib/ folder, but Tomcat already should provide you with those libraries. Remove them from your project and classpath. Search for that anywhere in your project or classpath and remove it.

    0 讨论(0)
提交回复
热议问题