在Apache Tomcat 8上调用RESTful服务将返回404

时间:2015-11-19 18:28:16

标签: java eclipse rest tomcat

我有Eclipse Mars和Dynamic Web项目以及Maven。 我为Eclipse安装并配置了Tomcat 8服务器。 我正在尝试对它运行REST请求。 Eclipse中没有编译错误或问题。 tomcat启动时没有SEVERE错误。 似乎所有的罐子都设置正确(我之前曾经与之斗争过)。我的所有java包都在src文件夹中。 Tomcat成功地从web.xml加载Java类 - 我已经检查了这个 本地主机:8080 /经理/ jmxproxy / QRY = %3Aj2eeType =的Servlet%2C

我有3个示例servlet - 并且它们都不起作用!

编辑:问题不在我的代码中,而是在Eclipse中!看到答案......

这是Tomcat启动日志:

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

这是我的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>

这是我的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> 

这是我的JERSEY java类:

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

这是我的java Application样本,它不在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));
  }
}

我的server.xml的一部分:

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

我的项目的WebContent文件夹中有index.xml,显示正常 本地主机:8080 / AutomationWeb / index.html的 我也有tomcat管理器应用程序 localhost:8080 / manager / html,我可以在那里看到我的应用程序

我试过了 本地主机:8080 / AutomationWeb / rest0 /你好 本地主机:8080 / AutomationWeb / rest1 /你好 localhost:8080 / AutomationWeb / rest2 / hello

本地主机:8080 / rest0 /你好 本地主机:8080 / rest1 /你好 localhost:8080 / rest2 / hello

localhost:8080 / stack / rest1 / hello

localhost:8080 / AutomationWeb / stack / rest2 / hello

以及其他一些变体,它们都返回HTTP 404代码

所以问题是: 泽西岛失败了吗?如何诊断?我的服务的URL是什么样的?是否可以在服务器上显示所有可用的URL?我错过了pom.xml中的依赖项吗?

我将非常感谢专家的帮助,一整天都在为此而战......

2 个答案:

答案 0 :(得分:0)

在编写部署描述符时,您会犯错误。

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

纠正第二个,即泽西休息服务,这应该有效

干杯!!

答案 1 :(得分:0)

我解决了这个问题。问题出在Eclipse中,而不是我的代码中。 自动构建或发布出错。

我在这里发布解决方案以帮助其他人:

  1. 停止tomcat服务器。

  2. 关闭自动构建。

  3. 刷新所有项目(项目上的右键 - >刷新)

  4. 执行清洁构建

  5. 按项目上的右键按钮,以正确的顺序(如果一个项目依赖于其他项目)手动构建每个项目 - &gt;构建

  6. 从tomcat webapps或wtpwebapps删除应用程序的文件夹

  7. eclipse中tomcat服务器上的右键 - &gt;清洁

  8. eclipse中tomcat服务器上的右键 - &gt;发布

  9. 确保在tomcat的wtpwebapps或webapps的应用程序文件夹中的web-inf中有classes文件夹和web.xml,无论你设置了什么

  10. 启动服务器

  11. 我的代码是正确的。浪费了一天。伙计们,Eclipse太痛苦了......

    更新:似乎问题是由进入循环的自动构建引起的。 我通过在我的maven web项目的.project文件中注释掉以下代码并重新启动eclipse(可能只是重启)来解决这个问题。

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

    现在自动构建和发布工作完美无缺!