gpt4 book ai didi

java - 将 jetty 与 Repast simphony 一起使用时出错

转载 作者:行者123 更新时间:2023-11-30 08:10:48 24 4
gpt4 key购买 nike

我想将 java websocket 与 Repast simphony 一起使用,但它不起作用,我收到以下错误:

    java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletContext()Ljavax/servlet/ServletContext;        at org.eclipse.jetty.websocket.servlet.UpgradeHttpServletRequest.(UpgradeHttpServletRequest.java:91)        at org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest.(ServletUpgradeRequest.java:52)        at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:156)        at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:148)        at org.eclipse.jetty.websocket.server.WebSocketHandler.handle(WebSocketHandler.java:97)        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)        at org.eclipse.jetty.server.Server.handle(Server.java:497)        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:248)        at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:610)        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:539)        at java.lang.Thread.run(Thread.java:745)

有人可以解释一下如何解决这个错误吗?谢谢

我测试了从互联网上获取的代码,如果我不使用 repast simphony 运行它,它就可以工作

我的服务器

    package webSocketTest;    import org.eclipse.jetty.server.Server;    import org.eclipse.jetty.websocket.server.WebSocketHandler;    import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;    public class WebSocketTest {        public static void main(String[] args) throws Exception {            Server server = new Server(8080);            WebSocketHandler wsHandler = new WebSocketHandler() {                @Override                public void configure(WebSocketServletFactory factory) {                    factory.register(MyWebSocketHandler.class);                }            };            server.setHandler(wsHandler);            server.start();            server.join();        }    }
    package webSocketTest;    import java.io.IOException;    import org.eclipse.jetty.websocket.api.Session;    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;    import org.eclipse.jetty.websocket.api.annotations.WebSocket;    @WebSocket    public class MyWebSocketHandler {        @OnWebSocketClose        public void onClose(int statusCode, String reason) {            System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);        }        @OnWebSocketError        public void onError(Throwable t) {            System.out.println("Error: " + t.getMessage());        }        @OnWebSocketConnect        public void onConnect(Session session) {            System.out.println("Connect: " + session.getRemoteAddress().getAddress());            try {                session.getRemote().sendString("Hello Webbrowser");            } catch (IOException e) {                e.printStackTrace();            }        }        @OnWebSocketMessage        public void onMessage(String message) {            System.out.println("Message: " + message);        }    }

最佳答案

移除 groovy jar 的另一种方法:事实上 Repast Simphony 2.2 使用 servlet-api 2.4。它不包含 javax.servlet.http.HttpServletRequest.getServletContext() 方法。另一种解决方案是将 Repast Simphony 默认使用的 servlet-api jar 替换为最近的 javax servlet-api

关于java - 将 jetty 与 Repast simphony 一起使用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31510279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com