6

I am trying to create a simple chat application using websockets. I followed this tutorial: https://www.baeldung.com/websockets-spring.

This is my config:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
         registry.addEndpoint("/chat");
         registry.addEndpoint("/chat").withSockJS();
    }
}
@Controller
public class WebSocketController {
    @MessageMapping("/chat")
    @SendTo("/topic/messages")
    public OutputMessage send(Message message) throws Exception {
        return new OutputMessage(message.getFrom(), message.getText(), Instant.now());
    }
}
@Builder
@Getter
public class Message {
    private final String from;
    private final String text;
}

@Builder
@Getter
public class OutputMessage {
    private final String from;
    private final String text;
    private final Instant date;
}

Now I am trying to send the message using Message request model.

enter image description here

but at the end I am getting:

2022-01-02 11:17:30.438 ERROR 26412 --- [nio-8080-exec-7] o.s.w.s.m.StompSubProtocolHandler        : Failed to parse TextMessage payload=[{
    "fr..], byteCount=44, last=true] in session 49df8471-5fe9-d785-c4c9-5058a712bb9b. Sending STOMP ERROR to client.

java.lang.IllegalArgumentException: No enum constant org.springframework.messaging.simp.stomp.StompCommand.{
    at java.base/java.lang.Enum.valueOf(Enum.java:240) ~[na:na]
    at org.springframework.messaging.simp.stomp.StompCommand.valueOf(StompCommand.java:28) ~[spring-messaging-5.3.14.jar:5.3.14]
    at org.springframework.messaging.simp.stomp.StompDecoder.decodeMessage(StompDecoder.java:148) ~[spring-messaging-5.3.14.jar:5.3.14]
    at org.springframework.messaging.simp.stomp.StompDecoder.decode(StompDecoder.java:115) ~[spring-messaging-5.3.14.jar:5.3.14]
    at org.springframework.messaging.simp.stomp.BufferingStompDecoder.decode(BufferingStompDecoder.java:114) ~[spring-messaging-5.3.14.jar:5.3.14]
    at org.springframework.web.socket.messaging.StompSubProtocolHandler.handleMessageFromClient(StompSubProtocolHandler.java:252) ~[spring-websocket-5.3.14.jar:5.3.14]

Should I send some additional headers that allow to parse the message? I am out of ideas.

konradDos
  • 81
  • 1
  • 5

1 Answers1

6

I followed the same tutorial for understanding WebSockets and was facing the exact same issue. I figured out that newline characters in the request body are causing the problem. Your request body must be:

{"from": "test", "text": "test"}

It worked for me after I made this change.

Yonatan Karp-Rudin
  • 1,056
  • 8
  • 24
Vatsal
  • 61
  • 1
  • 3
  • Hello I have the same problem, no \n in my message and server Error :No enum constant org.springframework.messaging.simp.stomp.StompCommand.{"from": "test","text": "Hello"} – Rick Jun 04 '23 at 17:01