在Eclipse中在Tomcat 7上运行Jersey Web应用程序时出现404错误 [英] 404 error when running Jersey web app on Tomcat 7 in Eclipse

查看:69
本文介绍了在Eclipse中在Tomcat 7上运行Jersey Web应用程序时出现404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Jersey创建一个简单的RESTful Web服务并将其部署在Tomcat 7上.由于我是Java Java Web应用程序开发的新手并且渴望学习它,因此我关注

Tomcat错误

您在代码中看到的一个红色十字架是完全在Java Build Path上找不到超类"javax.servlet.http.HttpServlet" ,该教程视频中的讲师要求忽略,因为Tomcat提供了HTTPServlet类.

控制台输出

  2016年5月1日晚上10:09:20 org.apache.tomcat.util.digester.SetPropertiesRule开始警告:[SetPropertiesRule] {服务器/服务/引擎/主机/上下文}将属性源"设置为"org.eclipse.jst.jee.server:messenger"找不到匹配的属性.2016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志INFO:服务器版本:Apache Tomcat/7.0.692016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志信息:服务器内置:2016年4月11日UTC2016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志INFO:服务器号:7.0.69.02016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志信息:操作系统名称:Mac OS X2016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志信息:操作系统版本:10.10.22016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志INFO:体系结构:x86_642016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志INFO:Java主页:/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home/jre2016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志信息:JVM版本:1.8.0_31-b132016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志INFO:JVM供应商:Oracle Corporation2016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志信息:CATALINA_BASE:/用户/abc/个人/Java/.metadata/.plugins/org.eclipse.wst.server.core/tmp02016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志信息:CATALINA_HOME:/用户/abc/个人/Java/apache-tomcat-7.0.692016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志INFO:命令行参数:-Dcatalina.base =/Users/abc/Personal/Java/.metadata/.plugins/org.eclipse.wst.server.core/tmp02016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志信息:命令行参数:-Dcatalina.home =/Users/abc/Personal/Java/apache-tomcat-7.0.692016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志INFO:命令行参数:-Dwtp.deploy =/Users/abc/Personal/Java/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps2016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志信息:命令行参数:-Djava.endorsed.dirs =/Users/abc/Personal/Java/apache-tomcat-7.0.69/endorsed2016年5月1日10:09:20 org.apache.catalina.startup.VersionLoggerListener日志INFO:命令行参数:-Dfile.encoding = UTF-82016年5月1日10:09:20 org.apache.catalina.core.AprLifecycleListener lifecycleEventINFO:在java.library.path:/Users/abc/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/上找不到基于APR的Apache Tomcat本机库,该库可在生产环境中实现最佳性能.Java/扩展:/System/Library/Java/扩展:/usr/lib/java:.2016年5月1日10:09:20 org.apache.coyote.AbstractProtocol初始化INFO:初始化ProtocolHandler ["http-bio-8080"]2016年5月1日10:09:20 org.apache.coyote.AbstractProtocol初始化INFO:初始化ProtocolHandler ["ajp-bio-8009"]2016年5月1日10:09:20 org.apache.catalina.startup.Catalina负载INFO:初始化在434毫秒内完成2016年5月1日10:09:20 org.apache.catalina.core.StandardService startInternalINFO:启动服务Catalina2016年5月1日晚上10:09:20 org.apache.catalina.core.StandardEngine startInternal信息:启动Servlet引擎:Apache Tomcat/7.0.692016年5月1日10:09:20 org.apache.coyote.AbstractProtocol开始INFO:启动ProtocolHandler ["http-bio-8080"]2016年5月1日10:09:20 org.apache.coyote.AbstractProtocol开始信息:启动ProtocolHandler ["ajp-bio-8009"]2016年5月1日10:09:20 org.apache.catalina.startup.Catalina开始INFO:服务器在269毫秒内启动 

pom.xml

 <项目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>信使</name>< build>< finalName> messenger</finalName>< plugins>< plugin>< groupId> org.apache.maven.plugins</groupId>< artifactId> maven-compiler-plugin</artifactId>< version> 2.5.1</version>< inherited>真</inherited><配置>< source> 1.7</source>< target> 1.7</target></configuration></plugin></plugins></build>< dependencyManagement><依赖关系><依赖关系>< groupId> org.glassfish.jersey</groupId>< artifactId>球衣-bom</artifactId>< version> $ {jersey.version}</version>< type> pom</type>< scope> import</scope></dependency></dependencies></dependencyManagement><依赖关系><依赖关系>< groupId> org.glassfish.jersey.containers</groupId>< artifactId>球衣容器servlet核心</artifactId><!-如果不需要servlet 2.x兼容性,请使用以下artifactId –<!-artifactId>球衣容器-servlet</artifactId-></dependency><!-取消注释以获取JSON支持<依赖关系>< groupId> org.glassfish.jersey.media</groupId>< artifactId>球衣-媒体-moxy</artifactId></dependency>-></dependencies><属性>< 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.作为Jakarta EE 9一部分的Jersey 3.x依赖于基于jakarta的API软件包.jakarta.servlet.Filter在Tomcat 10中可用.

解决方案->下载并使用Tomcat 10.或在Tomcat 9或更低版本中使用Jersey 2.X版.

我个人将Jersey 2.16与Tomcat 9一起使用,并且对我有用.

Trying to create a simple RESTful web service using Jersey and deploying it on Tomcat 7. Since I'm new to web app development in Java and eager to learn it, I'm following this tutorial.

I created this app as a Maven project in Eclipse and used the jersey-quickstart-webapp artifact version 2.22.2 with org.glassfish.jersey.archetypes groupID. Note that I had to later change the Project Facet to Dynamic Web Module else it wouldn't give me an option to deploy it to the local Tomcat server.

I"ve added my messenger app as a resource in Tomcat server. I don't see any error when I start the Tomcat server within Eclipse. There is just one WARNING WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:messenger' did not find a matching property., which, according to lots of SO posts, can be safely ignored.

After right-clicking on the app and clicking Run As -> Run on Server, I see a 404 page not found for the URL http://localhost:8080/messenger/ as well as http://localhost:8080/webapi/myresource.

Checked a lot of similar SO posts like this and this but they didn't help.

One thing I noticed is that the web.xml inside <peoject_root>/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ROOT/WEB-INF doesn't match with my app's web.xml(Not sure if it should match, after the app gets deployed to Tomcat). Infact the content under that dir is copied below:

<?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">

Need some pointers on what might be wrong. It might be something simple but I've spent a lot of time on this scratching my head and getting the same 404 error for the n'th time. I was hoping to get this simple app to work before I get some confidence and continue watching the training videos.

Adding some more detail via screenshots and file contents:

MyResource.java

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

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

project structure

Tomcat error

One RED cross you see in the code is acutally The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path which the instructor in the tutorial video asks to ignore since Tomcat provides the HTTPServlet class.

console output

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>

解决方案

I faced a similar problem and realized Jersey Version 3.0.0-M6 and Tomcat 9 were incompatible.

Reason --> Tomcat 9 uses javax.servlet.Filter. Jersey 3.x, part of Jakarta EE 9, depends on jakarta based API packages. jakarta.servlet.Filter is available in Tomcat 10.

Solution--> Downloading Tomcat 10 and using it. OR Using Jersey Version 2.X with Tomcat 9 or below.

I personally used Jersey 2.16 with Tomcat 9 and it worked for me.

这篇关于在Eclipse中在Tomcat 7上运行Jersey Web应用程序时出现404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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