2015-11-19 138 views
0

我已经将Eclipse Mars与Dynamic Web项目和Maven结合在一起。 我为Tomcat安装并配置了Tomcat 8服务器。 我正在尝试运行REST请求。 Eclipse中没有编译错误或问题。 当tomcat启动时没有SEVERE错误。 看来,所有的瓶子都被正确设置了(我之前曾经与之斗争过)。我所有的java包都在src文件夹中。 Tomcat的负载从Java web.xml中成功班 - 我在本地主机 选中此:?8080 /经理/ jmxproxy/QRY = %3Aj2eeType =的Servlet%2C在Apache Tomcat 8上调用RESTful服务返回404

我有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应用程序样本,是不是在web.xml中:我的server.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)); 
    } 
} 

部分:

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

我有INDEX.XML在我的项目的WebContent文件夹它显示在 localhost:8080/AutomationWeb/index.html 我也有tomcat管理器应用程序可在 本地主机:8080 /经理/ HTML,我可以看到我的应用程序

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

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

本地主机:8080 /堆叠/ rest1 /你好

本地主机:8080/AutomationWeb /堆叠/ rest2 /你好

一个nd一些其他的变化,他们都返回HTTP 404代码

所以问题是: 这是泽西岛失败?如何诊断?我的服务的网址是什么样的?是否有可能在服务器上显示所有可用的URL?我在pom.xml中缺少一个依赖项吗?

我将非常感谢专家的帮助,在这一整天的战斗...

回答

0

当您在部署描述符中编写代码时,您正在弄错。

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

正确的第二即泽西REST服务,并应工作

干杯!

+0

嗨,谢谢你的回答,但实际上有2个servlets与相应的映射定义!所以这不是一个错误... –

0

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

我在这里发表的解决方案,帮助他人:

  1. 停止Tomcat服务器。

  2. 关闭自动构建。按

  3. 执行干净的构建

  4. 构建手动每个项目按照正确的顺序(如果一个依赖于其他) -

  5. 刷新所有项目(>刷新项目右边的按钮)项目右键 - >打造

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

  7. 正确的,但在Eclipse吨Tomcat服务器上 - >清洁

  8. 在Eclipse Tomcat服务器上右键 - >发布

  9. 请确保您有classes目录和web.xml在Tomcat的应用程序文件夹的WEB-INF无论你设置了

  10. 启动服务器

我的代码wtpwebapps或web应用是正确的。浪费了一天的时间。伙计们,Eclipse是太痛苦了...

更新:似乎问题是由自动构建已进入循环引起的。 我注释掉下面在我的Maven web项目的项目文件的代码,并重新启动Eclipse的解决了这个(可能只需重新启动帮助)

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

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

相关问题