我正在使用 Spring 4 并遵循 Rossen Stoyanchev's blog post关于在 Spring 中使用 websockets。我能够让一切正常工作,但我不确定在发送 application/json
时使用自定义对象映射器的最佳方式是什么。
我正在注入(inject) SimpMessageSendingOperations
并调用 convertAndSend
。我不是很肯定,但我很确定我得到了一个包含 setMessageConverter
的 SimpMessagingTemplate
(它实现了 SimpMessageSendingOperations
)。此方法采用 MessageConverter
并且有一个 MappingJackson2MessageConverter
类,但当然它使用它自己的内部 ObjectMapper
,无法重新定义。
所以看起来我必须做的是创建一个自定义 MessageConverter
并在其中定义我的自定义 ObjectMapper
这样我就可以将它传递给 的实例SimpMessagingTemplate
然后我可以将其注入(inject)到我的类中。
这似乎可行,但也比我预期的更复杂。我是不是忽略了什么?
最佳答案
看起来是可行的,但在 Spring 4.0.1 中会变得更容易
参见 - https://jira.springsource.org/browse/SPR-11184
引用上面的错误报告。
In the mean time, with @EnableWebSocketMessageBroker setup you can:
- remove the annotation
- extend WebSocketMessageBrokerConfigurationSupport instead of implementing WebSocketMessageBrokerConfigurer
- override brokerMessageConverter() method and remember to keep @Bean in the overriding method
关于java - Spring 4 中 Websockets 的自定义对象映射器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20378765/