本文介绍了在 Tomcat 9 上运行时在 Maven/Jersey Web 服务上获取 java.lang.ClassNotFoundException:jakarta.servlet.Filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下资源在 Tomcat 9 服务器上运行时,出现错误 java.lang.ClassNotFoundException: jakarta.servlet.Filter

While running on Tomcat 9 server with the following resource, I am getting error java.lang.ClassNotFoundException: jakarta.servlet.Filter

以下是pom.xml文件

The following is the pom.xml file

<modelVersion>4.0.0</modelVersion>

<groupId>com.telusko</groupId>
<artifactId>demorest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>demorest</name>

<build>
    <finalName>demorest</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
    </dependency>


    <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-core -->


    <!-- uncomment this to get JSON support
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-binding</artifactId>
    </dependency>

    -->
</dependencies>
<properties>
    <jersey.version>3.0.0-M6</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </project>

以下是 demorest 项目的 web.xml 文件.

The following is the web.xml file with the demorest project.

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.telusko.demorest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

创建的资源文件如下:

package com.telusko.demorest;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("myresource")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

推荐答案

此类是 Servlet API 5.0 版的一部分,而后者又是 Jakarta EE 9 版的一部分.

This class is part of Servlet API version 5.0 which in turn is part of Jakarta EE version 9.

还有,

<jersey.version>3.0.0-M6</jersey.version>

这个 Jersey 版本基于 JAX-RS API 3.0 版,而后者又是 Jakarta EE 9 版的一部分.

this Jersey version is based off JAX-RS API version 3.0 which in turn is part of Jakarta EE version 9.

还有,

Tomcat 9 服务器

此 Tomcat 版本基于 Servlet API 版本 4.0,而后者又是 Java/Jakarta EE 版本 8 的一部分.

this Tomcat version is based off Servlet API version 4.0 which in turn is part of Java/Jakarta EE version 8.

所以,总而言之,目标 JEE 版本不匹配,这给您带来了麻烦.您有 2 个选择:

So, summarized, the targeted JEE versions don't match up and it's causing trouble for you. You have 2 options:

  1. 将 Jersey 降级到 2.x 版.这个基于 JAX-RS API 2.x 版,而后者又是 Java/Jakarta EE 8 版的一部分.

  1. Downgrade Jersey to version 2.x. This one is based off JAX-RS API version 2.x which in turn is part of Java/Jakarta EE version 8.

或者,将 Tomcat 升级到 10.x 版.这个基于 Servlet API 版本 5.0,而后者又是 Jakarta EE 版本 9 的一部分(反过来提供 JAX-RS API 版本 3.x,因此您可以使用预期的 Jersey 版本).

Or, upgrade Tomcat to version 10.x. This one is based off Servlet API version 5.0 which in turn is part of Jakarta EE version 9 (which in turn offers JAX-RS API version 3.x for which you can thus use the intended Jersey version).

技术原因是在从 Java/Jakarta EE 8 到 Jakarta EE 9 的步骤中,所有 javax.* 包都已重命名为 jakarta.* 包.因此,自 Jakarta EE 9 以来不再具有向后兼容性.目标运行时本身(因此,Tomcat 本身)必须真正成为 Jakarta EE 9 使用的 API 的目标.

The technical reason is that during the step from Java/Jakarta EE 8 to Jakarta EE 9 all javax.* packages have been renamed to jakarta.* packages. So there is no backwards compatibility anymore since Jakarta EE 9. The target runtime itself (thus, Tomcat itself), has really to be the one targeted for the APIs in use by Jakarta EE 9.

  • Apache Tomcat - Versions
  • Tomcat 9 casting servlets to javax.servlet.Servlet instead of jakarta.servlet.http.HttpServlet (this answer contains complete examples of proper pom.xml declarations for Tomcat 10+, Tomcat 9-, JEE 9+ and JEE 8-).

这篇关于在 Tomcat 9 上运行时在 Maven/Jersey Web 服务上获取 java.lang.ClassNotFoundException:jakarta.servlet.Filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 21:35