简体   繁体   English

带有jersey和tomcat的静态Web服务错误:404

[英]Restful webservices with jersey and tomcat error: 404

i'm working with eclipse creating a dynamic web project. 我正在与eclipse一起创建动态Web项目。 i'm using tomcat 7 and i've included the jar from jersey, here are the jars: 我正在使用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

my xml is: 我的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>

when running my project the trace is this: 运行我的项目时,跟踪是这样的:

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

so now i think my problem must be in the xml...what's wrong with it??? 所以现在我认为我的问题一定是在xml中。。。这怎么了???

This should be resolved by updating your Java version. 应该通过更新Java版本来解决。

The root cause is Unsupported major.minor version 51.0 . 根本原因是Unsupported major.minor version 51.0 This means, the class org/glassfish/jersey/servlet/init/JerseyServletContainerInitializer is complied for Java 7. So using Java 7 or higher will solve your problem. 这意味着org/glassfish/jersey/servlet/init/JerseyServletContainerInitializer是针对Java 7 org/glassfish/jersey/servlet/init/JerseyServletContainerInitializer 。因此,使用Java 7或更高版本将解决您的问题。

This is your problem: 这是你的问题:

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)

You need a higher version JDK . 您需要更高版本的JDK

The key to the problem is this line from the logging: 问题的关键是日志记录中的这一行:

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)

It means the JVM cannot read a compiled class file because it was compiled for a newer version of the Virtual Machine than the one you're currently running. 这意味着JVM无法读取已编译的类文件,因为它是为比当前正在运行的虚拟机更新的版本而编译的。

As you can read on Wikipedia , version 51.0 means the class is compiled for Java 7 or up. 正如您可以在Wikipedia上阅读的那样,版本51.0表示该类是为Java 7或更高版本编译的。 And indeed, as you can see in the Jersey docs , from Jersey 2.6 up, Java 7 is required. 确实,正如您在Jersey 2.6之前的Jersey docs中所看到 ,Java 7是必需的。

Your logging also reveals the current Java version being used: 您的日志记录还显示了当前使用的Java版本:

INFO: JVM Version:           1.6.0_45-b06

This is indeed too old. 这确实太旧了。 Running Java 7 or higher should solve your problem. 运行Java 7或更高版本应该可以解决您的问题。

Upto Jersey 2.6 you can run it with Java 6 (JDK 1.6). 直到Jersey 2.6,您都可以使用Java 6 (JDK 1.6)来运行它。

After version 2.6 + onwards you need (JDK 1.7) Java 7 . 2.6版本开始,您需要(JDK 1.7) Java 7

For Reference: This is my from project. 供参考:这是我的项目。 It is fully functional project please refer below. 它是功能齐全的项目,请参阅下面的内容。 I am using Jersey 2.6 because my production server has Java 6 so I had to lower it from Jersey 2.17 to 2.6. 我使用Jersey 2.6是因为我的生产服务器具有Java 6,所以我不得不将其从Jersey 2.17降低到2.6。

I invoke my sample webserive like this 我像这样调用我的示例Webserive

localhost:8080/OrderStatusWebService/rest/test/hello-world 

in my browser 在我的浏览器中

web.xml 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>

And this is a sample Webservice class 这是一个示例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();
    }


}

And since mine is a Maven project, this is my POM.xml. 而且由于我的项目是Maven项目,因此这是我的POM.xml。 Please note The property <finalName>OrderStatusWebService</finalName> 请注意,属性 <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>

First of all you shouldn't include below library into your classpath, as they available in tomcat library. 首先,您不应该在类路径中包括下面的库,因为它们在tomcat库中可用。 javax.servlet-api-3.0.1.jar javax.servlet-api-3.0.1.jar

Also, Please check for jersey-container-servlet-core.jar, jersey-container-servlet.jar libraries are really required in your case, otherwise prefer to delete those jar for you lib folder of project. 另外,请检查jersey-container-servlet-core.jar,在您的情况下确实需要jersey-container-servlet.jar库,否则请为项目的lib文件夹删除这些jar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM