java.lang.ClassCastException:org.glassfish.jersey.servlet.ServletContainer无法强制转换为javax.servlet.Servlet

时间:2014-11-13 10:23:29

标签: java maven servlets web pom.xml

由于以下错误,我无法在Tomcat 7.0服务器上启动我的webapp:

java.lang.ClassCastException: org.glassfish.jersey.servlet.ServletContainer cannot be cast to javax.servlet.Servlet

我的web.xml,包括ServlectContainer:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
 see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" 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">
<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>fr.tessi</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>

这是我的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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>fr.tessi</groupId>
<artifactId>cassandra-cli-back</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>cassandra-cli-back</name>

<build>
    <finalName>cassandra-cli-back</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>
    <dependency>
        <groupId>com.netflix.astyanax</groupId>
        <artifactId>astyanax-core</artifactId>
        <version>1.56.48</version>
    </dependency>
    <dependency>
        <groupId>com.netflix.astyanax</groupId>
        <artifactId>astyanax-cassandra</artifactId>
        <version>1.56.48</version>
    </dependency>
    <dependency>
        <groupId>com.netflix.astyanax</groupId>
        <artifactId>astyanax-thrift</artifactId>
        <version>1.56.48</version>
    </dependency>
    <!-- <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
    </dependency> -->

</dependencies>
<properties>
    <jersey.version>2.6</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jackson-2-version>2.4.3</jackson-2-version>
</properties>

尽管'servlet-api'依赖项提供了范围,但它仍然是在stratup上下载的。

已经尝试使用StackOverFlow和其他网站上的多个类来更改servlet类,但没有结果。它不断发送错误。

`

3 个答案:

答案 0 :(得分:1)

你的war文件中是否包含servlet-api.jar?如果是这样,请手动将其删除。

答案 1 :(得分:1)

我的同事解决了这个问题。基本上,依赖性冲突。这是功能依赖项列表:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <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.13</version>
    </dependency>
    <!-- Required only when you are using JAX-RS Client -->
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.13</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.13</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>


    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>
    <dependency>
        <!-- note: typically only ".0" patch version exists for core annotations -->
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>
    <dependency>




        <groupId>com.netflix.astyanax</groupId>
        <artifactId>astyanax-core</artifactId>
        <version>1.56.48</version>
    </dependency>
    <dependency>
        <groupId>com.netflix.astyanax</groupId>
        <artifactId>astyanax-cassandra</artifactId>
        <version>1.56.48</version>
        <exclusions>
            <exclusion>
                <artifactId>servlet-api</artifactId>
                <groupId>org.mortbay.jetty</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.netflix.astyanax</groupId>
        <artifactId>astyanax-thrift</artifactId>
        <version>1.56.48</version>
    </dependency>
    <!-- <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> 


        <version>1.6.1</version> </dependency> -->


</dependencies>
<properties>
    <jersey.version>2.13</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jackson-2-version>2.4.3</jackson-2-version>
</properties>

答案 2 :(得分:0)

需要Jersey-server依赖项,将其添加到您的pom.xml并尝试部署。

  <dependency>
     <groupId>org.glassfish.jersey.core</groupId>
     <artifactId>jersey-server</artifactId>
     <version>2.11</version>
  </dependency>