3

我正在尝试设置简单的 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)

看起来类路径有问题,但我不知道缺少哪些依赖项。

我的 pom.xml 中用于 Spark 的依赖项如下:

 <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");
}


}
4

1 回答 1

4

正如错误所暗示的,方法getHeaders在被调用的HttpServletResponse类中不存在。

这个方法从 Servlet 3.0 开始就存在了。

确保您的类路径不包含旧版本的 Servlet API,其中包括类 javax.servlet.http.HttpServletResponse 。

此外,您的依赖项之一可能包含它的旧版本。

于 2015-12-01T13:48:44.743 回答