Jetty、Jersey 和 Maven - Spring 错误
Jetty, Jersey, & Maven - Spring Error
所以我最近开始使用maven来管理依赖关系,并开始尝试使用Jersey来构建REST API。我发现了一个很棒的教程 here. A similar Whosebug question was posted here,第一个接受的响应具有相同的 maven 脚本。
我要部署到的服务器是 Jetty,所以我已经为 Jetty 安装了 eclipse 插件(Eclipse Jetty 3.8.0 使用 Jetty- 9.2.5) 并且过去仅将其用于 servlet 时没有遇到任何问题。当我修改 pom.xml 文件时,我执行了 Maven - Clean & Maven - Install ,所有内容 都出现了 将在启动时显示的 class 路径中适当列出。
所以我不太确定为什么它看不到那个 SpringServlet。此外,为什么在上面提供的链接中,它们主要使用 Jetty 时却引用 GlassFish 模块?
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.foo</groupId>
<artifactId>JerseyTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.5.v20141112</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.5.v20141112</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
</project>
错误:
2015-02-05 09:26:09.624:INFO::main: Logging initialized @102ms
Configuration: /var/folders/25/_t5gg17n7577_b94gh4nkh700000gn/T/eclipseJettyPlugin.config.JerseyTest.xml
Version: 9.2.5.v20141112
Context: /
Port: 8080
Classpath: /Users/me/.m2/repository/javax/annotation/javax.annotation-api/1.2/javax.annotation-api-1.2.jar
/Users/me/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar
/Users/me/.m2/repository/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar
/Users/me/.m2/repository/javax/ws/rs/javax.ws.rs-api/2.0/javax.ws.rs-api-2.0.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-continuation/9.1.1.v20140108/jetty-continuation-9.1.1.v20140108.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-http/9.2.5.v20141112/jetty-http-9.2.5.v20141112.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-io/9.2.5.v20141112/jetty-io-9.2.5.v20141112.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-security/9.2.5.v20141112/jetty-security-9.2.5.v20141112.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-server/9.2.5.v20141112/jetty-server-9.2.5.v20141112.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-servlet/9.2.5.v20141112/jetty-servlet-9.2.5.v20141112.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-util/9.2.5.v20141112/jetty-util-9.2.5.v20141112.jar
/Users/me/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.antlr/2.5.0/org.eclipse.persistence.antlr-2.5.0.jar
/Users/me/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.asm/2.5.0/org.eclipse.persistence.asm-2.5.0.jar
/Users/me/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.core/2.5.0/org.eclipse.persistence.core-2.5.0.jar
/Users/me/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.moxy/2.5.0/org.eclipse.persistence.moxy-2.5.0.jar
/Users/me/.m2/repository/org/glassfish/hk2/external/aopalliance-repackaged/2.2.0/aopalliance-repackaged-2.2.0.jar
/Users/me/.m2/repository/org/glassfish/hk2/external/javax.inject/2.2.0/javax.inject-2.2.0.jar
/Users/me/.m2/repository/org/glassfish/hk2/hk2-api/2.2.0/hk2-api-2.2.0.jar
/Users/me/.m2/repository/org/glassfish/hk2/hk2-locator/2.2.0/hk2-locator-2.2.0.jar
/Users/me/.m2/repository/org/glassfish/hk2/hk2-utils/2.2.0/hk2-utils-2.2.0.jar
/Users/me/.m2/repository/org/glassfish/hk2/osgi-resource-locator/1.0.1/osgi-resource-locator-1.0.1.jar
/Users/me/.m2/repository/org/glassfish/jersey/bundles/repackaged/jersey-guava/2.7/jersey-guava-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/containers/jersey-container-jetty-http/2.7/jersey-container-jetty-http-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/containers/jersey-container-servlet-core/2.7/jersey-container-servlet-core-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/core/jersey-client/2.7/jersey-client-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/core/jersey-common/2.7/jersey-common-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/core/jersey-server/2.7/jersey-server-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/ext/jersey-entity-filtering/2.7/jersey-entity-filtering-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/media/jersey-media-moxy/2.7/jersey-media-moxy-2.7.jar
/Users/me/.m2/repository/org/javassist/javassist/3.18.1-GA/javassist-3.18.1-GA.jar
/Users/me/Documents/workspace/JerseyTest/target/classes
2015-02-05 09:26:09.967:INFO:oejs.Server:main: jetty-9.2.5.v20141112
2015-02-05 09:26:11.199:WARN:oeja.AnnotationConfiguration:main: ServletContainerInitializers: detected. Class hierarchy: empty
2015-02-05 09:26:11.382:WARN:oejs.SecurityHandler:main: ServletContext@o.e.j.w.WebAppContext@1ed4004b{/,file:/Users/me/Documents/workspace/JerseyTest/src/main/webapp/,STARTING}{/Users/me/Documents/workspace/JerseyTest/src/main/webapp} has uncovered http methods for path: /
2015-02-05 09:26:11.412:WARN:oejs.BaseHolder:main:
java.lang.ClassNotFoundException: com.sun.jersey.spi.spring.container.servlet.SpringServlet
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">
<servlet>
<servlet-name>rest.service</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.foo.servlets</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest.service</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
谢谢!
您只是缺少正确的工件。
您的项目中根本不存在对 servlet com.sun.jersey.spi.spring.container.servlet.SpringServlet
的引用。
快速搜索 com.sun.jersey.spi.spring.container.servlet.SpringServlet
on search.maven.org 会显示包含 class 的工件。
您可能缺少依赖项,或者您不应该为您的设置使用那个特定的 servlet。想走哪条路,由您决定。
所以我最近开始使用maven来管理依赖关系,并开始尝试使用Jersey来构建REST API。我发现了一个很棒的教程 here. A similar Whosebug question was posted here,第一个接受的响应具有相同的 maven 脚本。
我要部署到的服务器是 Jetty,所以我已经为 Jetty 安装了 eclipse 插件(Eclipse Jetty 3.8.0 使用 Jetty- 9.2.5) 并且过去仅将其用于 servlet 时没有遇到任何问题。当我修改 pom.xml 文件时,我执行了 Maven - Clean & Maven - Install ,所有内容 都出现了 将在启动时显示的 class 路径中适当列出。
所以我不太确定为什么它看不到那个 SpringServlet。此外,为什么在上面提供的链接中,它们主要使用 Jetty 时却引用 GlassFish 模块?
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.foo</groupId>
<artifactId>JerseyTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.5.v20141112</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.5.v20141112</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
</project>
错误:
2015-02-05 09:26:09.624:INFO::main: Logging initialized @102ms
Configuration: /var/folders/25/_t5gg17n7577_b94gh4nkh700000gn/T/eclipseJettyPlugin.config.JerseyTest.xml
Version: 9.2.5.v20141112
Context: /
Port: 8080
Classpath: /Users/me/.m2/repository/javax/annotation/javax.annotation-api/1.2/javax.annotation-api-1.2.jar
/Users/me/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar
/Users/me/.m2/repository/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar
/Users/me/.m2/repository/javax/ws/rs/javax.ws.rs-api/2.0/javax.ws.rs-api-2.0.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-continuation/9.1.1.v20140108/jetty-continuation-9.1.1.v20140108.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-http/9.2.5.v20141112/jetty-http-9.2.5.v20141112.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-io/9.2.5.v20141112/jetty-io-9.2.5.v20141112.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-security/9.2.5.v20141112/jetty-security-9.2.5.v20141112.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-server/9.2.5.v20141112/jetty-server-9.2.5.v20141112.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-servlet/9.2.5.v20141112/jetty-servlet-9.2.5.v20141112.jar
/Users/me/.m2/repository/org/eclipse/jetty/jetty-util/9.2.5.v20141112/jetty-util-9.2.5.v20141112.jar
/Users/me/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.antlr/2.5.0/org.eclipse.persistence.antlr-2.5.0.jar
/Users/me/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.asm/2.5.0/org.eclipse.persistence.asm-2.5.0.jar
/Users/me/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.core/2.5.0/org.eclipse.persistence.core-2.5.0.jar
/Users/me/.m2/repository/org/eclipse/persistence/org.eclipse.persistence.moxy/2.5.0/org.eclipse.persistence.moxy-2.5.0.jar
/Users/me/.m2/repository/org/glassfish/hk2/external/aopalliance-repackaged/2.2.0/aopalliance-repackaged-2.2.0.jar
/Users/me/.m2/repository/org/glassfish/hk2/external/javax.inject/2.2.0/javax.inject-2.2.0.jar
/Users/me/.m2/repository/org/glassfish/hk2/hk2-api/2.2.0/hk2-api-2.2.0.jar
/Users/me/.m2/repository/org/glassfish/hk2/hk2-locator/2.2.0/hk2-locator-2.2.0.jar
/Users/me/.m2/repository/org/glassfish/hk2/hk2-utils/2.2.0/hk2-utils-2.2.0.jar
/Users/me/.m2/repository/org/glassfish/hk2/osgi-resource-locator/1.0.1/osgi-resource-locator-1.0.1.jar
/Users/me/.m2/repository/org/glassfish/jersey/bundles/repackaged/jersey-guava/2.7/jersey-guava-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/containers/jersey-container-jetty-http/2.7/jersey-container-jetty-http-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/containers/jersey-container-servlet-core/2.7/jersey-container-servlet-core-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/core/jersey-client/2.7/jersey-client-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/core/jersey-common/2.7/jersey-common-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/core/jersey-server/2.7/jersey-server-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/ext/jersey-entity-filtering/2.7/jersey-entity-filtering-2.7.jar
/Users/me/.m2/repository/org/glassfish/jersey/media/jersey-media-moxy/2.7/jersey-media-moxy-2.7.jar
/Users/me/.m2/repository/org/javassist/javassist/3.18.1-GA/javassist-3.18.1-GA.jar
/Users/me/Documents/workspace/JerseyTest/target/classes
2015-02-05 09:26:09.967:INFO:oejs.Server:main: jetty-9.2.5.v20141112
2015-02-05 09:26:11.199:WARN:oeja.AnnotationConfiguration:main: ServletContainerInitializers: detected. Class hierarchy: empty
2015-02-05 09:26:11.382:WARN:oejs.SecurityHandler:main: ServletContext@o.e.j.w.WebAppContext@1ed4004b{/,file:/Users/me/Documents/workspace/JerseyTest/src/main/webapp/,STARTING}{/Users/me/Documents/workspace/JerseyTest/src/main/webapp} has uncovered http methods for path: /
2015-02-05 09:26:11.412:WARN:oejs.BaseHolder:main:
java.lang.ClassNotFoundException: com.sun.jersey.spi.spring.container.servlet.SpringServlet
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">
<servlet>
<servlet-name>rest.service</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.foo.servlets</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest.service</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
谢谢!
您只是缺少正确的工件。
您的项目中根本不存在对 servlet com.sun.jersey.spi.spring.container.servlet.SpringServlet
的引用。
快速搜索 com.sun.jersey.spi.spring.container.servlet.SpringServlet
on search.maven.org 会显示包含 class 的工件。
您可能缺少依赖项,或者您不应该为您的设置使用那个特定的 servlet。想走哪条路,由您决定。