0

I'm using Spring Messaging and Spring Socket 4.0.5.RELEASE.

I would like to send a plain String message to the broker. It turns out that such a String is escaped, e.g. when doing the following on the server side:

private MessageSendingOperations<String> messagingTemplate;
messagingTemplate.convertAndSend("/app/someendpoint", "This is a String with a quotation mark: \". ");

then a subscribed STOMP client receives the following message:

<<< MESSAGE
content-type:application/json;charset=UTF-8
subscription:sub-0
message-id:l6dvrpro-3
destination:/app/someendpoint
content-length:46

"This is a String with a quotation mark: \". " 

So the payload includes surrounding quotation marks and escaped quotation marks.

How can I send an unescaped, "normal" String?

Abdull
  • 26,371
  • 26
  • 130
  • 172

2 Answers2

1

So, you don't want you message to be converted to JSON.

If you need it for all your messages, override WebSocketMessageBrokerConfigurer.configureMessageConverters() to exclude JSON converter from the list of active MessageConverters:

@Override
public boolean configureMessageConverters(List<MessageConverter> converters) {
    converters.add(new StringMessageConverter());
    return false; // Prevent registration of default converters
}

If you need it for this message only, try to specify its content type manually:

messagingTemplate.convertAndSend(
    "/app/someendpoint", 
    "This is a String with a quotation mark: \". ",
    Collections.singletonMap("content-type", "text/plain");
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • I gave your `messagingTemplate.convertAndSend(..)` a try but unfortunately in my case it still causes the client to receive a message with `content-type:application/json;charset=UTF-8` and with escaped strings... – Abdull Jun 24 '14 at 10:38
0

Ugly, but at least it is working, and doesn't need any specific WebSocketMessageBrokerConfigurer configuration:

String payload = "This is a String with a quotation mark: \". ";
byte[] decodedPayload = message.getBytes();

Map<String, Object> headers = new HashMap<>();
headers.put("content-type", "text/plain");
// alternatively, a JSON content type also works fine, e.g. when you need to send adhoc-generated custom JSON payloads: headers.put("content-type", "content-type:application/json;charset=UTF-8");
GenericMessage<byte[]> message = new GenericMessage<>(decodedPayload, headers);
messagingTemplate.send("/app/someendpoint", message);
Abdull
  • 26,371
  • 26
  • 130
  • 172