从Spring-Boot中的Websocket接收数据时调用服务时遇到麻烦
问题描述
我正在创建一个扩展TextWebSocketHandler
的处理程序,并能够打印从我的客户端在Flutter.io
中发送的数据。但是,当我尝试将数据传递给服务时,出现了一系列错误,如下所示:
Closing session due to exception for StandardWebSocketSession[id=9eb9b6b1-2fab-b27d-9ca1-22815c957e68, uri=ws://localhost:8080/name]
java.lang.NullPointerException: null
at offtop.Config.SocketHandler.lambda$0(SocketHandler.java:41) ~[classes/:na]
at java.base/java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:411) ~[na:na]
at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658) ~[na:na]
at offtop.Config.SocketHandler.handleTextMessage(SocketHandler.java:40) ~[classes/:na]
at org.springframework.web.socket.handler.AbstractWebSocketHandler.handleMessage(AbstractWebSocketHandler.java:43) ~[spring-websocket-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.socket.handler.WebSocketHandlerDecorator.handleMessage(WebSocketHandlerDecorator.java:75) ~[spring-websocket-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator.handleMessage(LoggingWebSocketHandlerDecorator.java:56) ~[spring-websocket-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator.handleMessage(ExceptionWebSocketHandlerDecorator.java:58) ~[spring-websocket-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.handleTextMessage(StandardWebSocketHandlerAdapter.java:114) ~[spring-websocket-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.access$000(StandardWebSocketHandlerAdapter.java:43) ~[spring-websocket-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:85) ~[spring-websocket-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:82) ~[spring-websocket-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.apache.tomcat.websocket.WsFrameBase.sendMessageText(WsFrameBase.java:395) ~[tomcat-embed-websocket-9.0.27.jar:9.0.27]
at org.apache.tomcat.websocket.server.WsFrameServer.sendMessageText(WsFrameServer.java:119) ~[tomcat-embed-websocket-9.0.27.jar:9.0.27]
at org.apache.tomcat.websocket.WsFrameBase.processDataText(WsFrameBase.java:495) ~[tomcat-embed-websocket-9.0.27.jar:9.0.27]
at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:294) ~[tomcat-embed-websocket-9.0.27.jar:9.0.27]
at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:133) ~[tomcat-embed-websocket-9.0.27.jar:9.0.27]
at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:82) ~[tomcat-embed-websocket-9.0.27.jar:9.0.27]
at org.apache.tomcat.websocket.server.WsFrameServer.doOnDataAvailable(WsFrameServer.java:171) ~[tomcat-embed-websocket-9.0.27.jar:9.0.27]
at org.apache.tomcat.websocket.server.WsFrameServer.notifyDataAvailable(WsFrameServer.java:151) ~[tomcat-embed-websocket-9.0.27.jar:9.0.27]
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.upgradeDispatch(WsHttpUpgradeHandler.java:148) ~[tomcat-embed-websocket-9.0.27.jar:9.0.27]
at org.apache.coyote.http11.upgrade.UpgradeProcessorInternal.dispatch(UpgradeProcessorInternal.java:54) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at java.base/java.lang.Thread.run(Thread.java:835) ~[na:na]
我首先认为这是我如何调用服务的问题,但是,我使用@Autowired,还尝试使用ApplicationContext来配置我的DI。现在我不确定是什么问题。如果Flutter.io提供了支持,我将使用STOMP和SockJS Websocket配置,现在我正在使用这些配置和类来处理Websocket的传入数据。
下面是我的代码
WSConfig.java |我如何配置我的Websocket
package offtop.Config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
@Configuration
@EnableWebSocket
public class WSConfig implements WebSocketConfigurer{
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new SocketHandler(), "/name");
}
}
SocketHandler.java |我如何处理传入的数据
package offtop.Config;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Stream;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import offtop.Services.WebsocketService;
@Component
public class SocketHandler extends TextWebSocketHandler {
@Autowired
private WebsocketService websocketService;
List sessions = new CopyOnWriteArrayList();
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message)
throws IOException {
for (int i = 0; i s = Stream.of(value.values().toString());
s.forEach(val -> {
websocketService.logData(val);
});
webSocketSession.sendMessage(new TextMessage("Received " + value.get("message") + " !"));
}
}
@Override
public void afterConnectionEstablished(final WebSocketSession session) throws Exception {
System.out.println("sessions: " + session.toString());
sessions.add(session);
}
}
WebsocketService.java |我用来处理来自websocket文本的传入数据的服务
package offtop.Services;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Service;
@Service
@Configurable
public class WebsocketService{
public String logData(String val){
System.out.println("val incoming: " + val);
return "works";
}
}
先谢谢您,如果要完全解决这个错误,请告诉我。
思路:
这里的问题是,它不会自动将WebsocketService
接线到SocketHandler
,因为您正在new
中使用registerWebSocketHandlers()
运算符。您需要在WSConfig
中自动装配socketHandler并使用自动装配的实例。
作为一般规则,永远不要在Spring托管bean上调用new
,而应将其自动装配。
您通常不应同时使用@Service
和@Configurable
,一个正在注册一个Spring bean,另一个正在告诉Spring您将创建自己的实例。在这种情况下,您应该只使用@Service