簡體   English   中英

在 Eclipse 中的 Tomcat 7 上運行 Jersey Web 應用程序時出現 404 錯誤

[英]404 error when running Jersey web app on Tomcat 7 in Eclipse

嘗試使用 Jersey 創建一個簡單的 RESTful Web 服務並將其部署在 Tomcat 7 上。由於我是 Java Web 應用程序開發的新手並渴望學習它,因此我正在關注本教程

我創造了這個應用程序是在Eclipse中Maven項目和使用的jersey-quickstart-webapp神器版本2.22.2org.glassfish.jersey.archetypes組ID。 請注意,我后來不得不將 Project Facet 更改為Dynamic Web Module否則它不會給我一個選項來將它部署到本地 Tomcat 服務器。

我已經將我的messenger應用程序添加為 Tomcat 服務器中的資源。當我在 Eclipse 中啟動 Tomcat 服務器時,我沒有看到任何錯誤。只有一個WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:messenger' did not find a matching property.根據很多 SO 帖子,可以安全地忽略該WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:messenger' did not find a matching property.

右鍵單擊應用程序並單擊Run As -> Run on Server ,我看到一個 404 頁面未找到 URL http://localhost:8080/messenger/以及http://localhost:8080/webapi/myresource

檢查了很多像這樣這樣的類似的SO帖子,但他們沒有幫助。

有一兩件事我注意到的是,在web.xml<peoject_root>/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ROOT/WEB-INF不匹配我的應用程序的web.xml (不確定它是否應該匹配,在應用程序部署到 Tomcat 之后)。 事實上,該目錄下的內容復制如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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" version="2.5">

需要一些關於可能出錯的提示。 這可能很簡單,但我花了很多時間在這上面撓頭,並且第 n 次出現相同的 404 錯誤。 我希望在我獲得一些信心並繼續觀看培訓視頻之前讓這個簡單的應用程序工作。

通過屏幕截圖和文件內容添加更多細節:

我的資源

package org.testapp.java.messenger;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.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!";
    }
}

網頁.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>org.testapp.java.messenger</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>

項目結構

在此處輸入圖片說明

Tomcat錯誤

在此處輸入圖片說明

您在代碼中看到的一個紅色十字實際上是The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path ,教程視頻中的講師要求忽略The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path ,因為 Tomcat 提供了 HTTPServlet 類。

控制台輸出

May 01, 2016 10:09:20 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:messenger' did not find a matching property.
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.69
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Apr 11 2016 07:57:09 UTC
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.69.0
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Mac OS X
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.10.2
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          x86_64
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/jre
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_31-b13
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /Users/abc/Personal/Java/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /Users/abc/Personal/Java/apache-tomcat-7.0.69
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/abc/Personal/Java/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Users/abc/Personal/Java/apache-tomcat-7.0.69
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/abc/Personal/Java/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/Users/abc/Personal/Java/apache-tomcat-7.0.69/endorsed
May 01, 2016 10:09:20 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
May 01, 2016 10:09:20 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/abc/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
May 01, 2016 10:09:20 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
May 01, 2016 10:09:20 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
May 01, 2016 10:09:20 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 434 ms
May 01, 2016 10:09:20 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
May 01, 2016 10:09:20 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.69
May 01, 2016 10:09:20 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
May 01, 2016 10:09:20 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
May 01, 2016 10:09:20 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 269 ms

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.testapp.java</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>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>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        -->
    </dependencies>
    <properties>
        <jersey.version>2.22.2</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

我遇到了類似的問題,並意識到 Jersey 版本 3.0.0-M6 和 Tomcat 9 不兼容。

原因 --> Tomcat 9 使用 javax.servlet.Filter。 Jersey 3.x 是 Jakarta EE 9 的一部分,它依賴於基於 Jakarta 的 API 包。 jakarta.servlet.Filter 在 Tomcat 10 中可用。

解決方案-->下載Tomcat 10並使用。 或 在 Tomcat 9 或更低版本中使用 Jersey 2.X 版。

我個人將 Jersey 2.16 與 Tomcat 9 一起使用,它對我有用。

在 web.xml 中,在 servlet 條目<display-name>messenger</display-name>之前添加以下行

現在試試你的服務http://localhost:8080/messenger/myresource

我在學習那個教程時也遇到了同樣的問題,嘗試了幾件事,但它不起作用。 最后,當我將版本降級到 2.6(部分試驗和錯誤)時,它顯示“知道了”文本作為響應。我最初為 Jersey 原型選擇了最新版本 3.0.0-M6。

更新:

我嘗試在 Jetty 上部署我的應用程序,方法是將以下插件添加到 pom.xml 中的構建部分。

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <version>6.1.10</version>
</plugin>

然后從終端運行mvn jetty:run 現在我可以在http://localhost:8080/messenger/webapi/myresource成功訪問我的網絡應用程序。 它返回Got it! 正如預期的那樣。

所以,這絕對是我將它部署到 tomcat 的方式的一個問題。 如果有人有一些指示,我有興趣進一步調試 tomcat 問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM