0

I've got a Spring-MVC controller using STOMP over websockets. Everything works fine apart from the String received in my STOMP JavaScript client upon subscription has been escaped, presumably by Spring.

All the examples I can find on the web and in official documentation uses POJOs for the return types and then uses Jackson JSON conversion auto-magically - I do not want this as the JSON I return is entirely dynamic - how do I switch this nonsense off so I can just return a plain string!?

@Controller
public class FooController {

    @SubscribeMapping("/foo")
    public String getUser() { 
        String json = customJsonConversion();
        return json;
    }

JSON received looks is in this form "{\"x\":1}" if output of customJsonConversion is {"x":1}

Adam
  • 35,919
  • 9
  • 100
  • 137

1 Answers1

1

Looks like you want to disable Jackson conversion. It is registered by default AbstractMessageBrokerConfiguration#brokerMessageConverter():

if (registerDefaults) {
    if (jackson2Present) {
        DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
        resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setContentTypeResolver(resolver);
        converters.add(converter);
    }
    converters.add(new StringMessageConverter());
    converters.add(new ByteArrayMessageConverter());
}

To disable that you should do this in your custom WebSocketMessageBrokerConfigurer:

public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
   messageConverters.add(new StringMessageConverter());
   messageConverters.add(new ByteArrayMessageConverter());
   return false;
}
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thanks. Ideally I'd like to mix and match with some controllers using raw JSON and others using POJOs, shame they didn't make it easier to configure at annotation level – Adam Jul 05 '14 at 08:29
  • Looking into this some more it seems that the way messageconverters work, i.e. with the isSupported() check, the StringMessageConverter should come before the Jackson converters anyway, then you support raw and jackson together. – Adam Jul 06 '14 at 07:06
  • SPR-11961 is now scheduled for 4.1. Regarding the mixing and matching, we could consider method-level control for content-type (e.g. an attribute on @SubscribeMapping). It would be useful to get a request that we can consider with a little more detail on one or two scenarios you have in mind. – Rossen Stoyanchev Jul 07 '14 at 13:23