Search code examples
springmavenspring-mvcjakarta-migration

java.lang.NoClassDefFoundError: javax/servlet/Filter is thrown during migration from Spring 4 to Spring 6


  java.lang.NoClassDefFoundError: javax/servlet/Filter

I have Added Jakarta servlet dependencies . I have also added CORS filter in my web.xml

The following is the web.xml declaration of CORS filter

<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    <init-param>
        <param-name>cors.allowed.origins</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.exposed.headers</param-name>
        <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.methods</param-name>
        <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.headers</param-name>
        <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
    </init-param>
</filter>

<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Solution

  • If you're using com.thetransactioncompany:cors-filter with version lower than 3.0, the com.thetransactioncompany.cors.CORSFilter is implementing javax.servlet.Filter which is not compatible with your upgrade going to Spring6 which requires jakarta-based components.

    You should upgrade the dependency to 3.0 to fix the issue; example of pom.xml extract below :

    <dependency>
        <groupId>com.thetransactioncompany</groupId>
        <artifactId>cors-filter</artifactId>
        <version>3.0</version>
    </dependency>