java.lang.IllegalStateException: A SockJsMessageCodec is required but not available:

3.4k views Asked by At

Hello Im new in websocket . I dont know why im getting this 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 

This is my 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>

For my Controller

@MessageMapping("/joinRoom")
@SendTo("/topic/lobby")
public List<Room> webSocketJoinRoom(Room room){
    System.out.println("your RoomName is: "+room.getRoomName());
    return roomService.getRooms();
}

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

My Jsp.

Room Name: <input type="text" class="roomName" value="${room.roomName}"/>
    <button class="joinRoom">Try it</button>

My Jar Files.

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

Am I missing something? the exception says that SockJsMessageCodec was not available..

please somebody help me thnx in advance..

BTW this is the demo I followed. https://gerrydevstory.com/2014/03/04/stock-ticker-demo-webapp-using-spring-4-websocket/

3

There are 3 answers

0
Secondo On BEST ANSWER

Hello Everyone I Found A Solution to my Problem...

I just added two jar files.

jackson-annotations-2.4.3.jar
jackson-core-2.4.3.jar

and it is now working...

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

<<< CONNECTED
version:1.1
heart-beat:0,0                stomp.js:145

connected to server undefined stomp.js:145
>>> SUBSCRIBE
id:sub-0
destination:/topic/lobby
1
luis-br On

Adding just jackson-databind:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.3</version>
        <scope>runtime</scope>
    </dependency>

worked for me.

0
LynAs On

Just an update for the community. I fixed this problem in my maven web application by adding the following dependency

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.0</version>
</dependency>