简体   繁体   中英

How to configure jersey to avoid com.sun.jersey.spi.container.servlet.ServletContainer giving problems?

I have managed to narrow down why I can't compile my project. It's a maven project meant to be an api REST, done on eclipse and the idea is for it to run on tomcat v6.

I had this configuration on the pom.xml:

<!-- Jersey -->
    <dependency>
     <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-server</artifactId>
         <version>1.19</version>
     </dependency>
     <dependency>
         <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-servlet</artifactId>
         <version>1.19</version>
     </dependency>

Which gives me the following error:

java.lang.ClassCastException: com.sun.jersey.spi.container.servlet.ServletContainer cannot be cast to javax.servlet.Servlet And when I've tried removing jersey-servlet, since I've seen people say that they create trouble and I only need jersey-server and so on, I get this:

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

I've been messing arround with a lot of configuration, trying to follow different solutions, but I believe the problem lies here. Could anyone tell me how to properly configure this?

My 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.CC.enterprise.rest.jersey</groupId>
  <artifactId>CC-HelloWorld</artifactId>
      <version>0.0.1-SNAPSHOT</version>

<distributionManagement>
    <!-- Will be used when it is NOT a snapshot version -->
    <repository>
        <id>releases</id>
        <name>RepositoryProxy</name>
        <url>edited</url>
    </repository>
    <!-- Will be used when IT IS a snapshot version -->
    <snapshotRepository>
        <id>snapshots</id>
        <name>RepositoryProxy</name>
        <url>edited</url>
    </snapshotRepository>
</distributionManagement>


<dependencies>

    <!-- Jersey -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.8</version>
    </dependency>

    <!-- MongoDB -->
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.2.2</version>
    </dependency>

    <!-- JSON -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.9</version>
    </dependency>

    <!-- Servlet API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

</dependencies>

My web.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 Web Application</display-name>



<servlet>
    <servlet-name>jersey-helloworld-serlvet</servlet-name>
    <servlet-class>
       com.sun.jersey.spi.container.servlet.ServletContainer
    </servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.CC.enterprise.rest.jersey</param-value>
    </init-param>
    <init-param>
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
      <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>jersey-helloworld-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

Try removing the jersey-server dependency. Use only the jersey-servlet dependency:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.19</version>
</dependency>

And ensure the com.sun.jersey dependencies are using the same version. At the time of writing, the most recent version is 1.19.1 .

If it's a new project, forget the old Jersey 1.x and go for Jersey 2.x .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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