我正在尝试使用 Jersey + Google 应用引擎构建一个简单的 hello world 应用程序两天。对于简单的 AppEngine 项目,我遵循了这些教程,两者都工作得很好 https://developers.google.com/appengine/docs/java/gettingstarted/creating https://developers.google.com/appengine/docs/java/webtoolsplatform
但现在我正在尝试添加 Jersey 并遵循本教程 http://www.vogella.com/articles/REST/article.html .
但是服务器一直给我
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
当我在 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>TestServer</display-name>
<servlet>
<servlet-name>Jersey REST Service</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.test.myproject</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
我已经从 here 下载了 Jersey JAX-RS 2.1 RI bundle
并已将所有 jar 文件添加到 WEB-INF/lib
文件夹中,如教程中所述。即使在两天后,也没有任何效果。我在谷歌上搜索了几次,显然使用 Maven 的人已经以某种方式解决了它,但我没有使用 Maven,编写该教程的人也没有。
只是为了检查导入的 Jersey jars
中是否存在 com.sun.jersey.spi.container.servlet.ServletContainer
我试着只写这个完全限定的名称Java 并让 intellisense 完成名称,但在 com.sun.je
之后我无法获得任何 intellisense 所以我最后的猜测是最新的 Jersey 中有一些包重新排列 build 和 jersey
不再位于 com.sun
中。我筋疲力尽,我将不胜感激任何帮助。
最佳答案
您已经下载了 Jersey 2(JAX-RS 2 的注册机)。您所指的教程使用 Jersey 1。从 ( here) 下载 Jersey 1.17.1 应该足够了。
Jersey 1 使用 com.sun.jersey
,而 Jersey 2 使用 org.glassfish.jersey
因此异常(exception)。
另请注意,以 com.sun.jersey
开头的 init-param
也不会被 Jersey 2 识别。
编辑
Registering Resources and Providers in Jersey 2包含有关如何在 Jersey 2 中注册类/实例的附加信息。
关于eclipse - java.lang.ClassNotFoundException : com. sun.jersey.spi.container.servlet.ServletContainer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18086218/