2015-11-19 113 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> 
--> 

現在自動構建和發佈完美的工作!