简体   繁体   中英

Calling RESTful services on Apache Tomcat 8 returns 404

I've got Eclipse Mars with Dynamic Web project and Maven. I have Tomcat 8 server installed and configured for Eclipse. I'm trying to run REST request on it. There is no compilation errors or problems in Eclipse. There is no SEVERE errors when tomcat starts. It seems that all jars are correctly set (I had a hard fight with this before). All my java packages are in src folder. Tomcat loads Java classes from web.xml successfully - I've checked this in localhost:8080/manager/jmxproxy/?qry= %3Aj2eeType=Servlet%2c

I have 3 sample servlets - and none of them work!

EDIT: The problem was not in my code but in Eclipse! See the answer...

Here is Tomcat start log:

??? 19, 2015 9:20:11 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:AutomationWeb' did not find a matching property.
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.28
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Oct 7 2015 18:25:21 UTC
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.28.0
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 7
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.1
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre7
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.7.0_79-b15
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\RPI automation\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\RPI automation\apache-tomcat-8.0.28
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\RPI automation\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\RPI automation\apache-tomcat-8.0.28
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\RPI automation\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\RPI automation\apache-tomcat-8.0.28\endorsed
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
??? 19, 2015 9:20:11 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\jre7\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\ThinkPad\Bluetooth Software\;C:\Program Files\ThinkPad\Bluetooth Software\syswow64;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Lenovo\Access Connections\;C:\Program Files (x86)\IBM\Personal Communications\;C:\Program Files (x86)\IBM\Trace Facility\;C:\Program Files (x86)\IBM\Mobility Client\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;.
??? 19, 2015 9:20:11 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
??? 19, 2015 9:20:11 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
??? 19, 2015 9:20:11 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
??? 19, 2015 9:20:11 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
??? 19, 2015 9:20:11 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 878 ms
??? 19, 2015 9:20:11 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
??? 19, 2015 9:20:11 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.28
??? 19, 2015 9:20:15 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
??? 19, 2015 9:20:15 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [228] milliseconds.
??? 19, 2015 9:20:22 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
??? 19, 2015 9:20:23 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\RPI automation\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps\host-manager
??? 19, 2015 9:20:26 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
??? 19, 2015 9:20:26 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\RPI automation\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps\host-manager has finished in 3,151 ms
??? 19, 2015 9:20:26 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\RPI automation\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps\manager
??? 19, 2015 9:20:30 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
??? 19, 2015 9:20:30 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\RPI automation\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps\manager has finished in 3,157 ms
??? 19, 2015 9:20:30 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
??? 19, 2015 9:20:30 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
??? 19, 2015 9:20:30 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 18768 ms

Here is my 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cybermesser.automation</groupId>
  <artifactId>automation</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
  <name>stack</name>
  <description>REST API</description>
  <build>
    <finalName>automation</finalName>
    <sourceDirectory>src</sourceDirectory>
    <plugins>    
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat8-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <url>http://yourhost:8080/manager/text</url>
        </configuration>
      </plugin>
    </plugins>
  </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
            <version>8.0.28</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
            <version>8.0.28</version>
        </dependency>       
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm-all</artifactId>
            <version>5.0.4</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.22.1</version>
        </dependency>       
        <dependency>
          <groupId>org.glassfish.jersey.containers</groupId>
          <artifactId>jersey-container-servlet-core</artifactId>
          <version>2.22.1</version>
        </dependency>
        <dependency>        
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.22.1</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
            <version>2.6.1</version>
        </dependency>      
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20150729</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
    </dependencies>
</project>

Here is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>automation</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <display-name>stack</display-name>


   <servlet>
    <servlet-name>MyRESTService</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <!-- Register resources and providers under com.vogella.jersey.first package. -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.cybermesser.web.restful</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyRESTService</servlet-name>
    <url-pattern>/rest1/*</url-pattern>
  </servlet-mapping>


 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <!-- Register resources and providers under com.vogella.jersey.first package. -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.vogella.jersey.first</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest2/*</url-pattern>
  </servlet-mapping>
</web-app> 

Here is my JERSEY java class:

package com.cybermesser.web.restful;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.cybermesser.db.queries.DBQueries;

@Path("/hello")
public class RESTService {

      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String sayPlainTextHello() {
        return "Hello Jersey";
      }
}

Here is my java Application sample that is not in web.xml:

package com.cybermesser.web.restful;


import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import com.cybermesser.web.restful.RESTService;

@ApplicationPath("/rest0")
public class RestApp extends Application {
  public Set<Class<?>> getClasses() {
    return new HashSet<Class<?>>(Arrays.asList(RESTService.class));
  }
}

part of my server.xml:

     <Context docBase="AutomationWeb" path="/AutomationWeb" reloadable="true" source="org.eclipse.jst.jee.server:AutomationWeb"/></Host>
    </Engine>
  </Service>
</Server>

I have index.xml in WebContent folder of my project and it displays fine at localhost:8080/AutomationWeb/index.html I also have tomcat manager app available at localhost:8080/manager/html and i can see my app there

I've tried localhost:8080/AutomationWeb/rest0/hello localhost:8080/AutomationWeb/rest1/hello localhost:8080/AutomationWeb/rest2/hello

localhost:8080/rest0/hello localhost:8080/rest1/hello localhost:8080/rest2/hello

localhost:8080/stack/rest1/hello

localhost:8080/AutomationWeb/stack/rest2/hello

and some other variations, all of them return HTTP 404 code

So the questions are: Is this Jersey failure? How to diagnose this? What does the URL to my service look like? And is it possible to display all available URLs on server? Am I missing a dependency in pom.xml?

I will be very grateful for the expert help, was fighting with this for the whole day...

You are making mistaking when you are writing in your deployment descriptor.

<servlet-name>MyRESTService</servlet-name>
<servlet-name>Jersey REST Service</servlet-name>

Correct the second ie Jersey Rest Service and that should work

Cheers!!

I solved this. The problem is somewhere in Eclipse, not in my code. Automatic build or publish gone wrong.

I post the solution here to help others:

  1. Stop the tomcat server.

  2. Turn off automatic build.

  3. Refresh all your projects (right button on project - > refresh)

  4. Perform clean build

  5. Build each of your projects manually in the correct order(if one dependent on other) by pressing right button on project -> build

  6. delete your application's folder from tomcat webapps or wtpwebapps

  7. right button on tomcat server in eclipse -> clean

  8. right button on tomcat server in eclipse -> publish

  9. make sure you have classes folder and web.xml in your web-inf in the application folder in tomcat's wtpwebapps or webapps whatever you've set up

  10. start the server

My code was correct. Wasted a day on it. Guys, Eclipse is soo much pain...

UPDATE: It seems that the problem was caused by automatic build that has gone into loop. I solved this by commenting out following code in the .project file of my maven web project and restarting eclipse (maybe just restart helped)

<!--
    <buildCommand>
        <name>org.eclipse.m2e.core.maven2Builder</name>
        <arguments>
        </arguments>
    </buildCommand> 
-->

Now automatic build and publish work perfectly!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM