question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto-configure ObjectMapper used in the WebSocket MappingJackson2MessageConverter

See original GitHub issue

I just wanted to include some cool websocket stuff within my project. This kinda worked out of the box! Thank you!!

I use SimpMessagingTemplate for sending messages. They get automatically converted to JSON, which is nice, BUT I can’t figure out which ObjectMapper is getting used for this, because it won’t use my custom Serializers and it also doesn’t use my custom Jackson settings from application.properties.

Should I just use @Autowired ObjectMapper and serialize my Objects to String by myself?

Btw: I didn’t test if it uses the correct ObjectMapper for JSON messages from the client to the server.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
benneqcommented, Feb 4, 2015

Yes, I’m using simply

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { ... }

I use AbstractWebSocketMessageBrokerConfigurer because I need to set the Endpoint and the Prefixes.


EDIT: I now use this code. Is this the right way?

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    @Autowired ObjectMapper objectMapper;

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry
            .addEndpoint("/stomp").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry
            .setApplicationDestinationPrefixes("/app")
            .setUserDestinationPrefix("/user")
            .enableSimpleBroker("/queue", "/topic");
    }

    @Override
    public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
        DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
        resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setObjectMapper(objectMapper);
        converter.setContentTypeResolver(resolver);
        messageConverters.add(converter);
        return false;
    }   
}
0reactions
joshdanhallcommented, Jan 8, 2017

I guess that might be an another approach, although as WebSocketMessageBrokerConfigurationSupport doesn’t implement WebSocketMessageBrokerConfigurer, I’m not sure how to wire it up…

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.lang.NoClassDefFoundError org.springframework ...
I am trying to open a WebSocket in Spring Client. ... initObjectMapper(MappingJackson2MessageConverter.java:92) ~[spring-messaging-5.2.5.
Read more >
org.springframework.boot.autoconfigure.websocket ...
org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration maven / gradle build tool code. The class is part of the package ...
Read more >
Spring Cloud
Spring Cloud AWS module dependencies can be used directly in Maven with a direct configuration ... MappingJackson2MessageConverter messageConverter = new.
Read more >
org.springframework.messaging.converter.MessageConverter
instanceOf(MappingJackson2MessageConverter.class)); }. Example #10 ... The converter is also used when sending a message to the message broker.
Read more >
[Solved]-Camel - RabbitMQ spring boot-Springboot
... How to configure Jackson ObjectMapper for Camel in Spring Boot ... spring boot rabbitmq MappingJackson2MessageConverter custom object conversion ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found