您好,我是websocket的新手。我不知道为什么我得到这个异常(exception)。
java.lang.IllegalStateException: A SockJsMessageCodec is required but not available: Add Jackson 2 to the classpath, or configure a custom SockJsMessageCodec.
at org.springframework.util.Assert.state(Assert.java:385)
at org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService.getMessageCodec(TransportHandlingSockJsService.java:130)
at org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession.handleMessage(WebSocketServerSockJsSession.java:187)
at org.springframework.web.socket.sockjs.transport.handler.SockJsWebSocketHandler.handleTextMessage(SockJsWebSocketHandler.java:92)
at org.springframework.web.socket.handler.AbstractWebSocketHandler.handleMessage(AbstractWebSocketHandler.java:43)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.handleTextMessage(StandardWebSocketHandlerAdapter.java:112)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.access$000(StandardWebSocketHandlerAdapter.java:42)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:82)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:79)
at org.apache.tomcat.websocket.WsFrameBase.sendMessageText(WsFrameBase.java:369)
at org.apache.tomcat.websocket.WsFrameBase.processDataText(WsFrameBase.java:468)
at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:272)
at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:116)
at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:54)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:192)
at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:178)
at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:92)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:601)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Output on my console
Opening Web Socket... stomp.js:145
Web Socket Opened... stomp.js:145
>>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000 stomp.js:145
Whoops! Lost connection to undefined
这是我的bjplayer-servlet.xml
<mvc:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="com.package.bjplayer"/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view"/>
<property name="suffix" value=".jsp"/>
</bean>
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/ws">
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>
对于我的 Controller
@MessageMapping("/joinRoom")
@SendTo("/topic/lobby")
public List<Room> webSocketJoinRoom(Room room){
System.out.println("your RoomName is: "+room.getRoomName());
return roomService.getRooms();
}
我的JavaScript
<script type="text/javascript">
var contextPath = "${pageContext.request.contextPath}";
var socket = new SockJS(contextPath+"/ws");
var stompClient = Stomp.over(socket);
var connectCallback = function() {
stompClient.subscribe('/topic/lobby', renderLobby);
};
var errorCallback = function(error) {
alert("Opps Mali ka Blehh");
};
stompClient.connect({}, connectCallback, errorCallback);
function renderLobby(response) {
var rooms = JSON.parse(response.body);
$('#lobby').empty();
//Somecodes....
}
$(document).ready(function() {
$('.joinRoom').click(function(e){
e.preventDefault();
var roomName = $('.roomName').val();
var jsonstr = JSON.stringify({'roomName': roomName});
stompClient.send("/app/joinRoom", {}, jsonstr);
return false;
});
});
我的Jsp。
Room Name: <input type="text" class="roomName" value="${room.roomName}"/>
<button class="joinRoom">Try it</button>
我的Jar文件。
spring-beans-4.1.0.RELEASE.jar
spring-context-4.1.0.RELEASE.jar
spring-context-support-4.1.0.RELEASE.jar
spring-core-4.1.0.RELEASE.jar
spring-expression-4.1.0.RELEASE.jar
spring-jdbc-4.1.0.RELEASE.jar
spring-jms-4.1.0.RELEASE.jar
spring-messaging-4.1.0.RELEASE.jar
spring-orm-4.1.0.RELEASE.jar
spring-tx-4.1.0.RELEASE.jar
spring-web-4.1.0.RELEASE.jar
spring-webmvc-4.1.0.RELEASE.jar
spring-websocket-4.1.0.RELEASE.jar
jackson-databind-2.3.4.jar
我想念什么吗?异常表明SockJsMessageCodec不可用。
请有人事先帮助我。
顺便说一句,这是我关注的演示。 https://gerrydevstory.com/2014/03/04/stock-ticker-demo-webapp-using-spring-4-websocket/
最佳答案
仅添加jackson-databind:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
<scope>runtime</scope>
</dependency>
为我工作。
关于spring - java.lang.IllegalStateException : A SockJsMessageCodec is required but not available:,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26988193/