[英]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.
这是一个旨在作为api REST的Maven项目,在eclipse上完成,其想法是使其在tomcat v6上运行。
I had this configuration on the pom.xml: 我在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.ClassCastException: com.sun.jersey.spi.container.servlet.ServletContainer cannot be cast to javax.servlet.Servlet
并且当我尝试删除jersey-servlet时,因为我看到人们说他们在制造麻烦而且我只需要jersey-server等,我得到这个:
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: 我的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: 我的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. 尝试删除
jersey-server
依赖性。 Use only the jersey-servlet
dependency: 仅使用
jersey-servlet
依赖项:
<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. 并确保
com.sun.jersey
依赖项使用相同的版本。 At the time of writing, the most recent version is 1.19.1
. 在撰写本文时,最新版本是
1.19.1
。
If it's a new project, forget the old Jersey 1.x and go for Jersey 2.x . 如果是新项目,请忘记旧的Jersey 1.x ,然后选择Jersey2.x 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.