Groovy + Dropwizard and servlet 3.1.0 issue



我正在尝试使用基于DropWizard Framework 0.8.1的Groovy 2.4.1创建一个简单的REST服务器。我用于此概念证明的时髦脚本是:

import com.codahale.metrics.annotation.Timed
import com.google.common.base.Optional
import io.dropwizard.Application
import io.dropwizard.Configuration
import io.dropwizard.setup.Bootstrap
import io.dropwizard.setup.Environment
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.QueryParam
import javax.ws.rs.core.MediaType
@Grapes([
        @Grab(group = 'io.dropwizard', module = 'dropwizard-core', version = '0.8.1')])
class RESTProviderConfiguration extends Configuration {
    String template
}
class RESTProviderApplication extends Application<RESTProviderConfiguration> {
    @Override
    String getName() {
        'hello-world'
    }
    @Override
    void initialize(Bootstrap<RESTProviderConfiguration> bootstrap) {
    }
    @Override
    void run(RESTProviderConfiguration configuration, Environment environment) {
        def resource = new HelloWorldResource(template: configuration.template);
        environment.jersey().register(resource);
    }
}

@Path('/hello-world')
@Produces(MediaType.APPLICATION_JSON)
class HelloWorldResource {
    String template
    @GET
    @Timed
    String sayHello(@QueryParam('name') Optional<String> name) {
        String.format(template, name.or('John Doe'))
    }
}
new RESTProviderApplication().run(args)

服务器启动,但在Resource/Hello-World上的请求下,抛出了以下异常:

WARN  [2015-05-11 12:17:04,099] org.eclipse.jetty.servlet.ServletHandler: Error for /hello-world
! java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getHeader(Ljava/lang/String;)Ljava/lang/String;
! at org.eclipse.jetty.servlets.GzipFilter.doFilter(GzipFilter.java:322) ~[jetty-servlets-9.2.9.v20150224.jar:9.2.9.v20150224]
! at io.dropwizard.jetty.BiDiGzipFilter.doFilter(BiDiGzipFilter.java:134) ~[dropwizard-jetty-0.8.1.jar:0.8.1]
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) ~[jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
! at io.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:29) ~[dropwizard-servlets-0.8.1.jar:0.8.1]
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) ~[jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
! at io.dropwizard.jersey.filter.AllowedMethodsFilter.handle(AllowedMethodsFilter.java:44) ~[dropwizard-jersey-0.8.1.jar:0.8.1]
! at io.dropwizard.jersey.filter.AllowedMethodsFilter.doFilter(AllowedMethodsFilter.java:39) ~[dropwizard-jersey-0.8.1.jar:0.8.1]
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) ~[jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) [jetty-servlet-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:240) [metrics-jetty9-3.1.1.jar:3.1.1]
! at io.dropwizard.jetty.RoutingHandler.handle(RoutingHandler.java:51) [dropwizard-jetty-0.8.1.jar:0.8.1]
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:95) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:159) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.Server.handle(Server.java:497) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) [jetty-io-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at java.lang.Thread.run(Thread.java:744) [na:1.7.0_45]
127.0.0.1 - - [11/May/2015:12:17:04 +0000] "GET /hello-world HTTP/1.1" 500 - "-" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Sa
fari/537.36" 22
WARN  [2015-05-11 12:17:04,104] org.eclipse.jetty.server.HttpChannel: /hello-world
! java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
! at com.codahale.metrics.jetty9.InstrumentedHandler.updateResponses(InstrumentedHandler.java:291) ~[metrics-jetty9-3.1.1.jar:3.1.1]
! at com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:254) ~[metrics-jetty9-3.1.1.jar:3.1.1]
! at io.dropwizard.jetty.RoutingHandler.handle(RoutingHandler.java:51) ~[dropwizard-jetty-0.8.1.jar:0.8.1]
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:95) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:159) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.Server.handle(Server.java:497) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) [jetty-io-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at java.lang.Thread.run(Thread.java:744) [na:1.7.0_45]
WARN  [2015-05-11 12:17:04,105] org.eclipse.jetty.util.thread.QueuedThreadPool:
! java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
! at org.eclipse.jetty.server.handler.ErrorHandler.handle(ErrorHandler.java:111) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.Response.sendError(Response.java:597) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.HttpChannel.handleException(HttpChannel.java:486) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.HttpConnection$HttpChannelOverHttp.handleException(HttpConnection.java:576) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:386) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) ~[jetty-io-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at java.lang.Thread.run(Thread.java:744) [na:1.7.0_45]
WARN  [2015-05-11 12:17:04,105] org.eclipse.jetty.util.thread.QueuedThreadPool: Unexpected thread death: org.eclipse.jetty.util.thread.QueuedThreadPool$3@5bdb85f9 in dw{STARTED,8<=
8<=1024,i=1,q=0}

这是因为Groovy class Path具有2.4 Servlet版本和Dropwizard 0.8.1的javax.servlet-api-api-3.1.0.jar。

您能告诉我如何首先更换槽罐或加载所需的版本吗?

我刚刚遇到了类似的问题。在我的情况下,问题是还有其他依赖性具有javax.servlet-api-2.5.0.jar,所以我有两个版本的javax.servlet-api

mvn dependency:tree -Dincludes=*:*servlet*
mystuff:mystuff:jar:1.0-SNAPSHOT
 mystuff:rest-support:jar:2.0.0-RELEASE:compile
 |  - com.github.kongchen:swagger-maven-plugin:jar:2.3.4:compile
 |     - javax.servlet:servlet-api:jar:2.5:compile
 ...
 - mystuff:myotherstuff:jar:2.2.4:compile
    - javax.servlet:javax.servlet-api:jar:3.1.0:compile

我通过手动将其从Mystuff的Maven依赖项中手动排除了摇动弹药

来修复它。

希望有帮助

您必须在build.gradle中添加 javax.servlet-api作为具有 3.1.0的显式依赖项。

这覆盖了Groovy提供的默认值。

dependencies {
   compile group: 'javax.servlet', name: 'javax.servlet-api', version:  '3.1.0'
   ...
}

一些选项:

  • 在运行Groovy脚本之前,请使用:eval $(grape resolve -shell javax.servlet javax.servlet-api 4.0.0)
  • 将Servlet API获取到classPath中
  • 或在$GROOVY_HOMElib中删除或替换servlet-api-2.4.jar(不建议)

相关内容

  • 没有找到相关文章

最新更新