servlet api版本和范围在maven中

时间:2013-09-18 01:31:05

标签: java spring tomcat servlets jetty

我在我的spring web应用程序中添加了一个过滤器。我正在使用jdk进行开发,所以我必须包含一个servlet-api jar来让我的项目进行编译。我的理解是servlet api及其实现应该由jetty(我正在开发的容器)和tomcat(prod服务器)提供。当我将以下代码段添加到我的pom.xml时,我的项目成功编译。我的环境设置如下:

Java 1.6 jdk Jetty 8.1.4.v20120524 Spring 3.2.0。发布

<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>servlet-api-2.5</artifactId>
    <version>6.1.14</version>
</dependency>

但是,当我在本地运行我的应用程序时,服务器在启动时会抛出以下异常:

com.sun.jersey.spi.inject.Errors$ErrorMessagesException
        at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
        at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
        at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
        at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
        at com.sun.jersey.spi.spring.container.servlet.SpringServlet.initiate(SpringServlet.java:117)
        at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318
)
        at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
        at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
        at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
        at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
        at javax.servlet.GenericServlet.init(GenericServlet.java:244)
        at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:492)
        at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:312)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
        at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:778)
        at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:258)
        at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1234)
        at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:699)
        at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
        at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:256)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
        at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224)
        at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:167)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
        at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
        at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:90)
        at org.eclipse.jetty.server.Server.doStart(Server.java:262)
        at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
        at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:511)
        at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:364)
        at org.mortbay.jetty.plugin.JettyRunMojo.execute(JettyRunMojo.java:516)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
2013-09-17 21:19:35.038:WARN:/:unavailable
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
        at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
        at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
        at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
        at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
        at com.sun.jersey.spi.spring.container.servlet.SpringServlet.initiate(SpringServlet.java:117)
        at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318
)
        at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
        at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
        at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
        at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
        at javax.servlet.GenericServlet.init(GenericServlet.java:244)
        at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:492)
        at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:312)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
        at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:778)
        at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:258)
        at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1234)
        at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:699)
        at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
        at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:256)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
        at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224)
        at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:167)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
        at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
        at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:90)
        at org.eclipse.jetty.server.Server.doStart(Server.java:262)
        at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
        at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:511)
        at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:364)
        at org.mortbay.jetty.plugin.JettyRunMojo.execute(JettyRunMojo.java:516)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at   org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at     org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

1 个答案:

答案 0 :(得分:1)

首先关于servlet-api我认为不需要你的项目依赖于jetty,最好是依赖api本身并按照已经提到的那样确定它的范围。

例如:

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>

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

关于你所遇到的错误的第二个,看起来你错过了一些依赖关系,你是否添加了泽西多部分?

<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>$your_version_of_jersey</version>
</dependency>