在Tomcat 9上运行时,在Maven/Jersey Web服务上获取java.lang.ClassNotFoundException:jakarta.servlet.Filter [英] Getting java.lang.ClassNotFoundException: jakarta.servlet.Filter on Maven/Jersey web service while running on Tomcat 9

查看:162
本文介绍了在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!";
    }
}

推荐答案

java.lang.ClassNotFoundException:jakarta.servlet.Filter

java.lang.ClassNotFoundException: jakarta.servlet.Filter

此类是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 9 server

此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.

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆