简体   繁体   English

Spring 4中Websockets的自定义对象映射器

[英]Custom object mapper for Websockets in Spring 4

I'm using Spring 4 and was following the Rossen Stoyanchev's blog post about using websockets in Spring. 我正在使用Spring 4,并且正在关注Rossen Stoyanchev关于在Spring中使用websockets 的博客文章 I was able to get everything working but I'm not sure what the best way to use a custom object mapper when sending application/json . 我能够使一切正常工作,但我不确定在发送application/json时使用自定义对象映射器的最佳方法是什么。

I'm injecting a SimpMessageSendingOperations and calling convertAndSend . 我注入SimpMessageSendingOperations并调用convertAndSend I'm not positive but I'm pretty sure I'm getting a SimpMessagingTemplate (it implements SimpMessageSendingOperations ) which contains a setMessageConverter . 我不是肯定的,但我很确定我正在获得一个包含setMessageConverterSimpMessagingTemplate (它实现了SimpMessageSendingOperations )。 This method takes a MessageConverter and there is a MappingJackson2MessageConverter class but of course it uses it's own internal ObjectMapper which cannot be redefined. 这个方法接受一个MessageConverter并且有一个MappingJackson2MessageConverter类,当然它使用它自己的内部ObjectMapper ,它无法重新定义。

So what it looks like I have to do is create a custom MessageConverter and define my custom ObjectMapper within it so I can pass it to an instance of SimpMessagingTemplate that I can then inject into my classes. 所以我要做的就是创建一个自定义MessageConverter并在其中定义我的自定义ObjectMapper ,这样我就可以将它传递给SimpMessagingTemplate一个实例,然后我可以将它注入到我的类中。

This seems like it would work, but also more involved than I expected. 这似乎可行,但也比我预期的更多。 Am I overlooking something? 我忽略了什么吗?

Looks like it is possible, but will be made easier in Spring 4.0.1 看起来有可能,但在Spring 4.0.1中会更容易

See - https://jira.springsource.org/browse/SPR-11184 请参阅 - https://jira.springsource.org/browse/SPR-11184

Quote from the bug report above. 引用上面的bug报告。

In the mean time, with @EnableWebSocketMessageBroker setup you can: 同时,使用@EnableWebSocketMessageBroker设置,您可以:

  1. remove the annotation 删除注释
  2. extend WebSocketMessageBrokerConfigurationSupport instead of implementing WebSocketMessageBrokerConfigurer 扩展WebSocketMessageBrokerConfigurationSupport而不是实现WebSocketMessageBrokerConfigurer
  3. override brokerMessageConverter() method and remember to keep @Bean in the overriding method 覆盖brokerMessageConverter()方法并记住将@Bean保留在重写方法中

Nowadays you can do it like this: 现在你可以这样做:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        // Avoid creating many ObjectMappers which have the same configuration.
        converter.setObjectMapper(getMyCustomObjectMapper());
        messageConverters.add(converter);

        // Don't add default converters.
        return false;
    }

    ...
}

Unfortunately ObjectMapper cannot be given directly to MappingJackson2MessageConverter 's constructor, meaning it will first create a useless ObjectMapper. 不幸的是, ObjectMapper不能直接给MappingJackson2MessageConverter的构造函数,这意味着它将首先创建一个无用的ObjectMapper。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM