Servlet >3.0 上的 NoSuchMethodError HttpServletRequest.getServletContext()

NoSuchMethodError HttpServletRequest.getServletContext() on Servlet >3.0

我正在尝试在 Jetty 中设置 WebSocketServlet。

我已经根据this guide, and implemented the WebSocket according to this one. However, when I run gradle jettyRun and attempt to open a connection (using this) to ws://localhost:8080/myProjectName/echo), I get java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletContext() (full stacktrace)

设置了一个servlet

see 这个方法只在 servlet 3.0 中引入 - 但是,因为我正在使用 servlet-api:3.1.0,我相信我应该有那个方法?

我有理由相信 WEB-INF/web.xml 中的路由是正确的,因为当我尝试点击另一个 url(例如 ws://localhost:8080/myProjectName/echo123)时,我没有在终端上得到错误输出。


文件:

build.gradle:

apply plugin: 'java'
apply plugin : 'jetty'

sourceCompatibility = 1.5
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    compile 'javax.servlet:javax.servlet-api:3.1.0'
    compile 'org.eclipse.jetty:jetty-server:9.3.2.v20150730'
    compile 'org.eclipse.jetty.websocket:websocket-servlet:9.0.0.M3'
    compile 'org.eclipse.jetty.aggregate:jetty-all:9.1.3.v20140225'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

    <servlet>
        <servlet-name>EchoServlet</servlet-name>
        <servlet-class>org.scubbo.myprojectname.EchoServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>EchoServlet</servlet-name>
        <url-pattern>/echo</url-pattern>
    </servlet-mapping>
</web-app>

src/main/java/org/scubbo/myprojectname/EchoServlet.java

package org.scubbo.myprojectname;

import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
import org.scubbo.myprojectname.sockets.EchoSocket;

public class EchoServlet extends WebSocketServlet {

    @Override
    public void configure(WebSocketServletFactory factory) {
        factory.getPolicy().setIdleTimeout(10000);
        factory.register(EchoSocket.class);
    }

src/main/java/org/scubbo/myprojectname/sockets/EchoSocket.java

package org.scubbo.myprojectname.sockets;

import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;

@WebSocket
public class EchoSocket {

    @OnWebSocketMessage
    public void onText(Session session, String message) {
        if (session.isOpen()) {
            System.out.printf("Echoing back message [%s]%n", message);
            session.getRemote().sendString(message, null);
        }
    }

}

您遇到的最初问题是您的类路径中有 2 个冲突版本的 servlet api(并不少见,因为该工件有六个奇怪的 maven 坐标,这会导致冲突maven 或 gradle)

难以解决

不要在您的项目中使用 jetty-all,该工件不适合在项目中使用。参见 past responses about this

您在 WebSocket 支持方面遇到的问题应该作为一个单独的问题提交到 Whosebug 上。

就您的 websocket 问题提出具体问题,我会在那里回答。