Spark: java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getHeaders

Created on 23 May 2016  路  5Comments  路  Source: perwendel/spark

It should be yet another issue with servlet version, very similar to #89 but that is a very old issue for v1.1 a should be already fixed

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getHeaders(Ljava/lang/String;)Ljava/util/Collection;
    at spark.utils.GzipUtils.checkAndWrap(GzipUtils.java:67)
    at spark.http.matching.Body.serializeTo(Body.java:69)
    at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:158)
    at spark.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:50)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119)
    at org.eclipse.jetty.server.Server.handle(Server.java:517)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:308)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
    at java.lang.Thread.run(Thread.java:745)

Versions:
java version "1.8.0_91"
Groovy Version: 2.4.6 JVM: 1.8.0_91 Vendor: Oracle Corporation OS: Linux

Script to replicate:

@Grapes([
    @Grab(group='com.sparkjava', module='spark-core', version='2.5'),
    @Grab(group='org.slf4j', module='slf4j-simple', version='1.7.12')
    ])
import static spark.Spark.*

get '/hello', { req, res -> 'Hello' }

this is the verbose output for Grapes

Resolving dependency: org.slf4j#slf4j-simple;1.7.12 {default=[default]}
Resolving dependency: com.sparkjava#spark-core;2.5 {default=[default]}
Preparing to download artifact org.slf4j#slf4j-simple;1.7.12!slf4j-simple.jar
Preparing to download artifact com.sparkjava#spark-core;2.5!spark-core.jar
Preparing to download artifact org.slf4j#slf4j-api;1.7.13!slf4j-api.jar
Preparing to download artifact org.eclipse.jetty#jetty-server;9.3.6.v20151106!jetty-server.jar
Preparing to download artifact org.eclipse.jetty#jetty-webapp;9.3.6.v20151106!jetty-webapp.jar
Preparing to download artifact org.eclipse.jetty.websocket#websocket-server;9.3.6.v20151106!websocket-server.jar
Preparing to download artifact org.eclipse.jetty.websocket#websocket-servlet;9.3.6.v20151106!websocket-servlet.jar
Preparing to download artifact javax.servlet#javax.servlet-api;3.1.0!javax.servlet-api.jar
Preparing to download artifact org.eclipse.jetty#jetty-http;9.3.6.v20151106!jetty-http.jar
Preparing to download artifact org.eclipse.jetty#jetty-io;9.3.6.v20151106!jetty-io.jar
Preparing to download artifact org.eclipse.jetty#jetty-util;9.3.6.v20151106!jetty-util.jar
Preparing to download artifact org.eclipse.jetty#jetty-xml;9.3.6.v20151106!jetty-xml.jar
Preparing to download artifact org.eclipse.jetty#jetty-servlet;9.3.6.v20151106!jetty-servlet.jar
Preparing to download artifact org.eclipse.jetty#jetty-security;9.3.6.v20151106!jetty-security.jar
Preparing to download artifact org.eclipse.jetty.websocket#websocket-common;9.3.6.v20151106!websocket-common.jar
Preparing to download artifact org.eclipse.jetty.websocket#websocket-client;9.3.6.v20151106!websocket-client.jar
Preparing to download artifact org.eclipse.jetty.websocket#websocket-api;9.3.6.v20151106!websocket-api.jar

so it looks like servlet v3.1.0 is correctly loaded...

Most helpful comment

Manually download servlet-api 3.1 jar and specify it as the "-cp" command line argument when you run your script. This raises the priority of classes in the jar when Groovy loads them.

e.g.
$ groovy -cp path/to/javax.servlet-api-3.1.0.jar YourScript.groovy

All 5 comments

I have the same issue here
Groovy Version: 2.4.7 JVM: 1.8.0_102 Vendor: Oracle Corporation OS: Windows 7

Minimal groovy script to reproduce the error:

@Grapes(   @Grab(group='com.sparkjava', module='spark-core', version='2.3')  )
import static spark.Spark.*
get '/hello', { req, res -> 'Hello from your groovy Sparkberry Pi!' }

When started the necessary dependencies are downloaded to ~.groovygrapes which then also contains servlet v.3.1.0 But still I get the same error.

[qtp23040335-26] WARN org.eclipse.jetty.server.HttpChannel - //localhost:4567/hello
java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getHeaders(Ljava/lang/String;)Ljava/util/Collection;
        at spark.utils.GzipUtils.checkAndWrap(GzipUtils.java:64)
        at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:251)
        at spark.webserver.JettyHandler.doHandle(JettyHandler.java:61)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119)
        at org.eclipse.jetty.server.Server.handle(Server.java:517)

This is strange:

Why is the groovy compiler complaining about that???

Found one more reference #523
This seems to be a Groovy problem. 523 mentions, that it shall be solved in groovy 2.5

Manually download servlet-api 3.1 jar and specify it as the "-cp" command line argument when you run your script. This raises the priority of classes in the jar when Groovy loads them.

e.g.
$ groovy -cp path/to/javax.servlet-api-3.1.0.jar YourScript.groovy

I dont believe this to be a groovy issue as suggested. I faced this issue when running a plain java solution. I also built this via shadowJar and ran that jar with java -jar. The jar includes the right jetty 9.3.6.v20151106. however there appears to be some classpath issue which loads the older servlet-api

In my case i found that the a builtin gradle plugin (jetty) was messing up the classpath. This was due to having 'compile gradleApi()' in dependencies. Issue was resolved by removing this.

Was this page helpful?
0 / 5 - 0 ratings