Maven Jersey 项目(HTTP 状态 500 – jakarta/servlet/Filter)

huangapple go评论85阅读模式
英文:

Maven Jersey Project (HTTP Status 500 - jakarta/servlet/Filter)

问题

我在 Eclipse IDE 中创建了一个 Maven 项目,我的机器上安装的 JDK 版本是 12。我添加了 Tomcat 服务器 v8.0。

当我在 "http://localhost:8080/messenger/" 上单击 "Jersey resource" 时,会抛出以下错误。

请指导我如何解决下面的错误,如果您需要其他详细信息,请告诉我。

HTTP 状态 500 - jakarta/servlet/Filter
类型 异常报告

消息 jakarta/servlet/Filter

描述 服务器遇到了内部错误,阻止了对此请求的完成。

异常

java.lang.NoClassDefFoundError: jakarta/servlet/Filter
	java.base/java.lang.ClassLoader.defineClass1(本机方法)
	java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
	java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
	org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2541)
	org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:858)
	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1301)
	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
	java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	java.base/java.lang.Thread.run(Thread.java:835)
根本原因

java.lang.ClassNotFoundException: jakarta.servlet.Filter
	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332)
	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
	java.base/java.lang.ClassLoader.defineClass1(本机方法)
	java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
	java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
	org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2541)
	org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:858)
	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1301)
	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
	java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	java.base/java.lang.Thread.run(Thread.java:835)
注意 根本原因的完整堆栈跟踪在 Apache Tomcat/8.0.36 日志中可用。

Apache Tomcat/8.0.36

以下是 POM.XML:

<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 http://maven.apache.org/maven-v4_0_0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>org.raghu.javabrains</groupId>
    <artifactId>messenger</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>messenger</name>
    
    <build>
        <finalName>messenger</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>12</source>
                    <target>12</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>
            <!-- 如果您不需要 servlet 2.x 兼容性,请使用以下 artifactId -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
        </dependency>
        <!-- 解除注释以获取 JSON 支持
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId

<details>
<summary>英文:</summary>

I have created Maven project in Eclipse IDE, JDK version installed on my machine is 12.
Added Tomcat server v8.0.

When I click &quot;Jersey resource&quot; on &quot;http://localhost:8080/messenger/&quot; it throws below error.

Please guide me how I can resolve below error and do let me know if you need additional details.

    HTTP Status 500 - jakarta/servlet/Filter
    type Exception report
    
    message jakarta/servlet/Filter
    
    description The server encountered an internal error that prevented it from fulfilling this request.
    
    exception
    
    java.lang.NoClassDefFoundError: jakarta/servlet/Filter
    	java.base/java.lang.ClassLoader.defineClass1(Native Method)
    	java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
    	java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
    	org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2541)
    	org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:858)
    	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1301)
    	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
    	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    	org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    	java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    	java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	java.base/java.lang.Thread.run(Thread.java:835)
    root cause
    
    java.lang.ClassNotFoundException: jakarta.servlet.Filter
    	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332)
    	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
    	java.base/java.lang.ClassLoader.defineClass1(Native Method)
    	java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
    	java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
    	org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2541)
    	org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:858)
    	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1301)
    	org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
    	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    	org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    	java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    	java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	java.base/java.lang.Thread.run(Thread.java:835)
    note The full stack trace of the root cause is available in the Apache Tomcat/8.0.36 logs.
    
    Apache Tomcat/8.0.36


POM.XML below

    &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
             xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
    
        &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    
        &lt;groupId&gt;org.raghu.javabrains&lt;/groupId&gt;
        &lt;artifactId&gt;messenger&lt;/artifactId&gt;
        &lt;packaging&gt;war&lt;/packaging&gt;
        &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
        &lt;name&gt;messenger&lt;/name&gt;
    
        &lt;build&gt;
            &lt;finalName&gt;messenger&lt;/finalName&gt;
            &lt;plugins&gt;
                &lt;plugin&gt;
                    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                    &lt;version&gt;2.5.1&lt;/version&gt;
                    &lt;inherited&gt;true&lt;/inherited&gt;
                    &lt;configuration&gt;
                        &lt;source&gt;12&lt;/source&gt;
                        &lt;target&gt;12&lt;/target&gt;
                    &lt;/configuration&gt;
                &lt;/plugin&gt;
            &lt;/plugins&gt;
        &lt;/build&gt;
    
        &lt;dependencyManagement&gt;
            &lt;dependencies&gt;
                &lt;dependency&gt;
                    &lt;groupId&gt;org.glassfish.jersey&lt;/groupId&gt;
                    &lt;artifactId&gt;jersey-bom&lt;/artifactId&gt;
                    &lt;version&gt;${jersey.version}&lt;/version&gt;
                    &lt;type&gt;pom&lt;/type&gt;
                    &lt;scope&gt;import&lt;/scope&gt;
                &lt;/dependency&gt;
            &lt;/dependencies&gt;
        &lt;/dependencyManagement&gt;
    
        &lt;dependencies&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
                &lt;artifactId&gt;jersey-container-servlet-core&lt;/artifactId&gt;
                &lt;!-- use the following artifactId if you don&#39;t need servlet 2.x compatibility --&gt;
                &lt;!-- artifactId&gt;jersey-container-servlet&lt;/artifactId --&gt;
            &lt;/dependency&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.glassfish.jersey.inject&lt;/groupId&gt;
                &lt;artifactId&gt;jersey-hk2&lt;/artifactId&gt;
            &lt;/dependency&gt;
            &lt;!-- uncomment this to get JSON support
            &lt;dependency&gt;
                &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
                &lt;artifactId&gt;jersey-media-json-binding&lt;/artifactId&gt;
            &lt;/dependency&gt;
            --&gt;
        &lt;/dependencies&gt;
        &lt;properties&gt;
            &lt;jersey.version&gt;3.0.0-M6&lt;/jersey.version&gt;
            &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;/properties&gt;
    &lt;/project&gt;



</details>


# 答案1
**得分**: 1

你需要将Jersey降级到2.x版本。

    <jersey.version>2.31</jersey.version>

Tomcat 9及更早版本不支持Jakarta EE,要支持Jakarta EE,必须使用尚未在eclipse中支持的tomcat 10。

完成以上更改后,
你需要将**MyResources.java**中的导入从jakarta更改为javax,如下所示。

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

更改为

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

希望对你有所帮助。

<details>
<summary>英文:</summary>

You need to downgrade Jersey to version 2.x.

    &lt;jersey.version&gt;2.31&lt;/jersey.version&gt;

The tomcat 9 and older version does not support Jakarta EE for support Jakarta EE you must use tomcat 10 which is **NOT** supported in eclipse yet.

Once you have done the above change.
You need to change the imports from jakarta to javax in **MyResources.java**.
as shown below.

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

 change to

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

Hope it helps.

</details>



huangapple
  • 本文由 发表于 2020年8月18日 15:59:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63464248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定