Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordering of MessageConverters in AbstractMessageBrokerConfiguration [SPR-11961] #16577

Closed
spring-projects-issues opened this issue Jul 6, 2014 · 1 comment
Assignees
Labels
in: web type: enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Jul 6, 2014

Adam Granger opened SPR-11961 and commented

I wish to have a controller with like this, which returns an already-encoded JSON String.

@Controller
public class FooController {

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

However this is picked up by Jackson and escaped. e.g. {"foo":123} becomes {"foo":123}. Does it ever make sense to escape a single String as JSON, surely at the top level JSON can only ever start with an object or an array? See http://www.ietf.org/rfc/rfc4627.txt

The ordering is specified in AbstractMessageBrokerConfiguration#brokerMessageConverter() puts Jackson before String and ByteArray converters

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

I can provide my own via WebSocketMessageBrokerConfigurer, however I believe that the out-of-the-box ordering in brokerMessageConverter() should be changed to the following:

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

Affects: 4.0.4

Referenced from: commits 670c216

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 6, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants