【发布时间】:2016-02-22 01:15:26
【问题描述】:
我有带有动态 Web 项目和 Maven 的 Eclipse Mars。 我为 Eclipse 安装和配置了 Tomcat 8 服务器。 我正在尝试对其运行 REST 请求。 Eclipse 中没有编译错误或问题。 tomcat 启动时没有严重错误。 似乎所有的罐子都正确设置了(我之前为此进行了一场艰苦的战斗)。我所有的 java 包都在 src 文件夹中。 Tomcat 成功地从 web.xml 加载 Java 类 - 我已经检查过了 localhost:8080/manager/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";
}
}
这是我不在 web.xml 中的 java 应用程序示例:
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/hello 本地主机:8080/AutomationWeb/rest1/hello 本地主机:8080/AutomationWeb/rest2/hello
localhost:8080/rest0/hello 本地主机:8080/rest1/你好 本地主机:8080/rest2/你好
localhost:8080/stack/rest1/hello
localhost:8080/AutomationWeb/stack/rest2/hello
以及其他一些变体,它们都返回 HTTP 404 代码
所以问题是: 这是泽西的失败吗?如何诊断?我的服务的 URL 是什么样的?是否可以在服务器上显示所有可用的 URL?我是否缺少 pom.xml 中的依赖项?
我将非常感谢专家的帮助,为此奋斗了一整天......
【问题讨论】: