Search code examples
javaspring-bootmavenjsptomcat10

ConfigServletWebServerApplicationContext: nested exception is java.lang.NoClassDefFoundError: jakarta/servlet/jsp/JspFactory using Tomcat 10 - Spring


I am trying to make the SpringBoot tutorial from Youtube. But in IntelliJ + Maven, not Eclipse. At the 1 hour mark, they add the jasper dependency and then run the program and it works nicely.

Before I add the jasper dependency, I have no errors. After I add it, I get this error:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.NoClassDefFoundError: jakarta/servlet/jsp/JspFactory. 

This is my main App File:


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class CoursesWebApp {

    public static void main(String[] args) {
        SpringApplication.run(CoursesWebApp.class, args);
    }

}

and the controller file

package com.example.springboot;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CoursesController {

    @RequestMapping("/courses")
    public void coursesDisplay() {
        System.out.println("Welcome to courses!");
        //return "course.jsp";
    }
}

This is my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot</name>
    <description>Project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>10.0.8</version>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

I have installed Tomcat version 10.0.8 and matched the Jasper with the same version.

I tried to make public class CoursesWebApp extends SpringBootServletInitializer, didn't work.

There is the annotation for @SpringBootApplication.

I tried to add

<dependency>
       <groupId>org.apache.tomcat.embed</groupId>
       <artifactId>tomcat-embed-jasper</artifactId>
       <scope>provided</scope>
   </dependency>

With scope changed to default or compile, didn't work.

I am utterly new at this and I have no idea how to fix it. It feels like it's something small, but I can't see it.

Thanks in advance for your help!


Solution

  • Tomcat 10 implements the Servlet 5.0 and JSP 3.0 APIs which Spring Boot does not yet support. You should use Tomcat 9.0.x, which implements Servlet 4.0 and JSP 2.3, instead.