我正在尝试设置简单的Spark REST示例,如下所述: http://sparkjava.com/documentation.html
当我浏览到: http://localhost:4567/hello
我收到此错误:
[qtp346024113-15] 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)
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)
类路径看起来有问题,但我不知道缺少哪些依赖项。
我的Spark的pom.xml中的依赖项如下:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.3.6.v20151106</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>9.3.6.v20151106</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.3.6.v20151106</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<version>9.3.6.v20151106</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-server</artifactId>
<version>9.3.3.v20150827</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-servlet</artifactId>
<version>9.3.3.v20150827</version>
</dependency>
代码:
package com.hello.rest.api;
import static spark.Spark.*;
public class RestAPI {
public RestAPI() {
}
public static void main(String[] args) {
System.out.println("Processing hello");
get("/hello", (req, res) -> "Hello World");
}
}
答案 0 :(得分:3)
正如错误所暗示的那样,调用的 HttpServletResponse 类中不存在 getHeaders 方法。
自Servlet 3.0起,此方法确实存在。
确保您的类路径不包含旧版本的Servlet API,其中包括类javax.servlet.http.HttpServletResponse。
此外,您的某个依赖项可能包含旧版本。