JAX-RS Tomcat 9.0.21 404 – 访问资源时未找到

问题描述 投票:0回答:1

我正在尝试使用 JDK 17、Maven 3.9.5、Tomcat 9.0.21 来学习 JAX-RS。我使用 GlassFish 中的原型

jersey-quickstart-webapp
来生成一个项目。 我只是构建了项目并部署了项目。我尝试通过单击下面屏幕截图中的文本“Jersey Resource”来访问资源,但收到了
404
(对于网址:
http://localhost:8080/messenger/webapi/myresource
):

enter image description here

以上

index.jsp
:

<html>
<body>
    <h2>Jersey RESTful Web Application!</h2>
    <p><a href="webapi/myresource">Jersey resource</a>
    <p>Visit <a href="http://jersey.java.net">Project Jersey website</a>
    for more information on Jersey!
</body>
</html>

我的资源定义:

package in.net.myproj.practice.javabrains;

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 映射在

web.xml
:

<?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>in.net.hrs.practice.javabrains</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>

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>in.net.myproj.practice</groupId>
    <artifactId>messenger</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>messenger</name>

    <build>
        <finalName>messenger</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <inherited>true</inherited>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.4.0</version>
            </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>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId>
        </dependency>
        -->
    </dependencies>
    <properties>
        <jersey.version>4.0.0-M1</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <war.mvn.plugin.version>3.4.0</war.mvn.plugin.version>
    </properties>
</project>

我所有的 servlet 映射、配置等似乎都按顺序进行。所以,我不知道我做错了什么。我搜索了很多线程,但在代码中找不到任何相关错误。

tomcat jersey jax-rs http-status-code-404
1个回答
0
投票

(1) Tomcat 9 支持 Java EE 8,但不支持 Jakarta EE。

(2) MyResource.java 使用 import jakarta.ws.xxx。 Tomcat 9 不支持。

(3) 检查 Tomcat/logs /localhost.2025-01-19.log ,您应该看到以下消息:

org.apache.catalina.core.StandardContext.loadOnStartup Servlet [Jersey Web Application] in web application [/messenger] threw load() exception java.lang.ClassNotFoundException: jakarta.servlet.Filter.

修复-1

使用 Tomcat 10.1 - Jakarta EE 10

FIX-2 添加依赖项

pom.xml

添加这些

<dependency>
    <groupId>jakarta.activation</groupId>
    <artifactId>jakarta.activation-api</artifactId>
    <version>2.1.2</version>
</dependency>

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>3.0.1</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>3.0.1</version>
</dependency>

WEB-INF/web.xml

修复包名

    <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>
            <!-- Wrong package name -->
            <!--
            <param-value>in.net.hrs.practice.javabrains</param-value>
            -->                   
            <!-- MyResource.java package name -->
            <param-value>in.net.myproj.practice.javabrains</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
© www.soinside.com 2019 - 2024. All rights reserved.