Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

java - 404 error when running Jersey web app on Tomcat 7 in Eclipse

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

enter image description here

Tomcat error

enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...