java - 在 eclipse 中的 tomcat 9 上运行 jersey-quickstart-webapp 时出现 404

标签 java eclipse maven tomcat jersey-2.0

在 Eclipse 中,我使用 org.glassfish 原型(prototype)“jersey-quickstart-webapp”创建一个新的 Maven 项目

pom.xml(注意源版本和目标版本一开始都是1.7,我改成了1.8)

<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>com.valak</groupId>
    <artifactId>mydemo</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>mydemo</name>

    <build>
        <finalName>mydemo</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.8</source>
                    <target>1.8</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>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId>
        </dependency>
        -->
    </dependencies>
    <properties>
        <jersey.version>2.30</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

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>com.valak.mydemo</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>

MyResource.java(在 com.valak.mydemo 包中):

package com.valak.mydemo;

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!";
    }
}

然后我右键单击该项目,“运行方式”>“运行配置”>“apache tomcat 9”。我去http://localhost:8080/mydemo/webapi/myresource从我的浏览器中获取 404,以防万一我尝试 http://localhost:8080/webapi/myresource仍然得到 404。

这是 Eclipse 的“控制台”选项卡中显示的内容,我没有看到任何错误:

Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name:   Apache Tomcat/9.0.30
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Dec 7 2019 16:42:04 UTC
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.30.0
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 10
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jdk1.8.0_231\jre
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_231-b11
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\Users\Valak\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         E:\Download da chrome win10\apache-tomcat-9.0.30
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\Valak\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=E:\Download da chrome win10\apache-tomcat-9.0.30
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\Valak\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=E:\Download da chrome win10\apache-tomcat-9.0.30\endorsed
Feb 07, 2020 11:55:22 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Feb 07, 2020 11:55:22 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: [C:\Program Files\Java\jdk1.8.0_231\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jdk-13/bin/server;C:/Program Files/Java/jdk-13/bin;C:\Program Files\Java\jdk1.8.0_231;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Users\Valak\AppData\Local\Microsoft\WindowsApps;C:\Users\Valak\Downloads\gradle-6.0.1\bin;;C:\Windows\system32;;.]
Feb 07, 2020 11:55:23 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Feb 07, 2020 11:55:23 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Feb 07, 2020 11:55:23 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [679] milliseconds
Feb 07, 2020 11:55:23 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Feb 07, 2020 11:55:23 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.30]
Feb 07, 2020 11:55:23 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Feb 07, 2020 11:55:23 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Feb 07, 2020 11:55:23 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [264] milliseconds

最佳答案

如何解决:

1)右键>属性>项目方面>勾选动态Web模块4

2)右键单击>属性>部署程序集>添加源:“src/main/webapp”和部署路径“/”。当服务器运行时,它会在根文件夹“/”中查找 WEB-INF 和 index.jsp,而不是在 src/main/webapp 中,并且 Eclipse 不会自动将文件从该源文件夹部署到“/”

3) 右键单击​​ > 属性 > 部署程序集 > 添加 java 构建路径条目(在 Eclipse 错误报告中,建议反对此解决方案,因为它应该由 Eclipse 自动完成,但对我来说不是这样)

右键>运行为>在服务器上运行> tomcat 9,就会出现主页,而不是tomcat的404。点击“jersey资源”将引导您到/webapi/myresource并返回成功文本“Got it”

关于java - 在 eclipse 中的 tomcat 9 上运行 jersey-quickstart-webapp 时出现 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60122286/

相关文章:

java - 表达式树 : Postfix to Infix

java - Android:转换为 Dalvik 格式失败:无法执行 dex:null

java - 如何在POM文件中指定maven命令行选项?

java - 这段代码是否正确使用了 ReentrantReadWriteLock?

java - 具有要在 Excel 中显示的不同数据的列表

java - 用于读取 HDf5 错误的 Netcdf java 库

Eclipse 文件搜索缓存

eclipse - 如何让 Eclipse 获取 Tomcat 类路径中的所有资源?

java - Jenkins 构建失败 : cannot resolve sqljdbc4 jar

java - 未找到 JSF 属性