我正在使用eclipse创建一个动态的Web项目。 我使用的是tomcat 7,我还包括了来自球衣的罐子,这里是罐子:
aopalliance-repackaged-2.4.0-b31.jar
asm-debug-all-5.0.4.jar
hk2-api-2.4.0-b31.jar
hk2-locator-2.4.0-b31.jar
hk2-utils-2.4.0-b31.jar
javassist-3.18.1-GA.jar
javax.annotation-api-1.2.jar
javax.inject-2.4.0-b31.jar
javax.servlet-api-3.0.1.jar
javax.ws.rs-api-2.0.1.jar
jaxb-api-2.2.7.jar
jersey-client.jar
jersey-common.jar
jersey-container-servlet-core.jar
jersey-container-servlet.jar
jersey-guava-2.21.jar
jersey-media-jaxb.jar
jersey-server.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar
persistence-api-1.0.jar
validation-api-1.1.0.Final.jar
我的xml是:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>RESTful Jersey Hello World</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>
<servlet>
<servlet-name>Restful</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.lorenzo.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Restful</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
运行我的项目时,跟踪是这样的:
set 21, 2015 12:30:52 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
AVVERTENZA: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Restful' did not find a matching property.
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Server version: Apache Tomcat/7.0.64
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Server built: Aug 19 2015 17:18:06 UTC
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Server number: 7.0.64.0
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: OS Name: Windows XP
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: OS Version: 5.1
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Architecture: x86
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Java Home: C:\Programmi\Java\jdk1.8.0_60\jre
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: JVM Version: 1.8.0_60-b27
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: JVM Vendor: Oracle Corporation
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: CATALINA_BASE: C:\Documents and Settings\gcappella\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: CATALINA_HOME: C:\Programmi\Apache Software Foundation\Tomcat 7.0
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Command line argument: -Dcatalina.base=C:\Documents and Settings\gcappella\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Command line argument: -Dcatalina.home=C:\Programmi\Apache Software Foundation\Tomcat 7.0
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Command line argument: -Dwtp.deploy=C:\Documents and Settings\gcappella\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Command line argument: -Djava.endorsed.dirs=C:\Programmi\Apache Software Foundation\Tomcat 7.0\endorsed
set 21, 2015 12:30:52 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMAZIONI: Command line argument: -Dfile.encoding=Cp1252
set 21, 2015 12:30:52 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFORMAZIONI: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Programmi\Java\jdk1.8.0_60\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Programmi/Java/jdk1.8.0_60/bin/../jre/bin/client;C:/Programmi/Java/jdk1.8.0_60/bin/../jre/bin;C:/Programmi/Java/jdk1.8.0_60/bin/../jre/lib/i386;C:\Programmi\Java\jdk1.8.0_60\bin;C:\oracle\ora92\bin;C:\Programmi\Oracle\jre\1.1.8\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Programmi\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;C:\Programmi\Wave Systems Corp\Gemalto\Access Client\v5\;C:\Programmi\Gemplus\GemSafe Libraries\BIN;C:\Programmi\File comuni\Roxio Shared\DLLShared\;C:\Programmi\File comuni\Roxio Shared\9.0\DLLShared\;C:\Programmi\ZipGenius 6\;c:\Programmi\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Serena\Dimensions 14.1\CM\prog;C:\Programmi\Microsoft\Web Platform Installer\;C:\Programmi\Git\cmd;C:\Programmi\Git\mingw32\bin;C:\Programmi\Git\usr\bin;ANT_HOME%\bin;C:\Programmi\nodejs\;C:\Documents and Settings\gcappella\Dati applicazioni\npm;C:\eclipse;;.
set 21, 2015 12:30:52 PM org.apache.coyote.AbstractProtocol init
INFORMAZIONI: Initializing ProtocolHandler ["http-bio-8080"]
set 21, 2015 12:30:52 PM org.apache.coyote.AbstractProtocol init
INFORMAZIONI: Initializing ProtocolHandler ["ajp-bio-8009"]
set 21, 2015 12:30:52 PM org.apache.catalina.startup.Catalina load
INFORMAZIONI: Initialization processed in 835 ms
set 21, 2015 12:30:52 PM org.apache.catalina.core.StandardService startInternal
INFORMAZIONI: Starting service Catalina
set 21, 2015 12:30:52 PM org.apache.catalina.core.StandardEngine startInternal
INFORMAZIONI: Starting Servlet Engine: Apache Tomcat/7.0.64
set 21, 2015 12:30:53 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFORMAZIONI: validateJarFile(C:\Documents and Settings\gcappella\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\Restful\WEB-INF\lib\javax.servlet-api-3.0.1.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
set 21, 2015 12:30:54 PM org.apache.catalina.startup.TldConfig execute
INFORMAZIONI: 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.
set 21, 2015 12:30:55 PM org.apache.coyote.AbstractProtocol start
INFORMAZIONI: Starting ProtocolHandler ["http-bio-8080"]
set 21, 2015 12:30:55 PM org.apache.coyote.AbstractProtocol start
INFORMAZIONI: Starting ProtocolHandler ["ajp-bio-8009"]
set 21, 2015 12:30:55 PM org.apache.catalina.startup.Catalina start
INFORMAZIONI: Server startup in 3587 ms
所以现在我觉得我的问题必须在xml中...它有什么问题???
答案 0 :(得分:3)
这应该通过更新Java版本来解决。
根本原因是Unsupported major.minor version 51.0
。这意味着,类org/glassfish/jersey/servlet/init/JerseyServletContainerInitializer
符合Java 7.因此使用Java 7或更高版本将解决您的问题。
答案 1 :(得分:2)
这是你的问题:
Caused by: java.lang.UnsupportedClassVersionError: org/glassfish/jersey/servlet/init/JerseyServletContainerInitializer : Unsupported major.minor version 51.0 (unable to load class org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer)
答案 2 :(得分:2)
问题的关键是来自日志记录的这一行:
Caused by: java.lang.UnsupportedClassVersionError: org/glassfish/jersey/servlet/init/JerseyServletContainerInitializer : Unsupported major.minor version 51.0 (unable to load class org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer)
这意味着JVM无法读取已编译的类文件,因为它是针对比您当前运行的虚拟机更新版本的虚拟机编译的。
正如您在Wikipedia上所看到的,版本51.0意味着该类是为Java 7或更高版本编译的。事实上,正如你在the Jersey docs中看到的那样,从Jersey 2.6开始,Java 7是必需的。
您的日志记录还会显示当前使用的Java版本:
INFO: JVM Version: 1.6.0_45-b06
这确实太旧了。运行Java 7或更高版本可以解决您的问题。
答案 3 :(得分:2)
Upto Jersey 2.6 您可以使用 Java 6 (JDK 1.6)运行它。
在版本2.6 + 之后,您需要(JDK 1.7) Java 7 。
供参考: 这是我的项目。它是功能齐全的项目请参考下面。 我使用的是Jersey 2.6,因为我的生产服务器有Java 6,所以我不得不将它从Jersey 2.17降低到2.6。
我像这样调用我的示例webserive
localhost:8080/OrderStatusWebService/rest/test/hello-world
在我的浏览器中
<强>的web.xml 强>
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Order Status REST WebService</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.xxx.orderstatus.config.OrderStatusConfig</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.scanning.recursive</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
这是一个示例Webservice类
package com.xxx.orderstatus.webservice;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
@Path("/test")
public class TestWebService
{
@GET
@Path("/hello-world")
public Response checkAuth(@QueryParam("userId") String userId) {
return Response.status(200).entity("Hello").build();
}
}
由于我的是Maven项目,这是我的POM.xml。 请注意属性 <finalName>OrderStatusWebService</finalName>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx.orderstatus</groupId>
<artifactId>OrderStatusWebService</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>OrderStatusWebService Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.6</jdk.version>
<jstl.version>1.2</jstl.version>
<jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version>
</properties>
<repositories>
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
<repository>
<id>snapshot-repository.java.net</id>
<name>Java.net Snapshot Repository for Maven</name>
<url>https://maven.java.net/content/repositories/snapshots/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<!-- My Dependencies START -->
<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.6</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- JDBC -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<!-- GSON -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<!-- These jars dont' come by default with the latest Jerser Server Container
Jars -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- Apache Commons Codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!-- SLF4j and Log4j Libs -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<!-- Apache Commons IO -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- Multipart Dependencies -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.6</version>
</dependency>
<!-- Commons DBCP 1.4 Starts -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- My Dependencies ENDS -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>OrderStatusWebService</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
答案 4 :(得分:0)
首先,你不应该将下面的库包含在你的类路径中 在tomcat库中可用。 的javax.servlet-API-3.0.1.jar
另外,请检查jersey-container-servlet-core.jar,在你的情况下确实需要jersey-container-servlet.jar库,否则更喜欢为你的lib文件夹删除那些jar。