I'm experimenting with Spring 4 WebSocket STOMP application. Is there a way to explicitly specify content type of the returned message produced by a handler? By default the handler below produces application/json
and processed by corresponding message converter.
@Controller
public class ProductController {
@MessageMapping("/products/{id}")
public String getProduct(@DestinationVariable int id) {
return getProductById(id);
}
}
I'm looking for something like @RequestMapping(produces = "text/xml")
in Spring MVC.
UPDATE (reply to Rossen's answer):
Ideally I would like to be able to return both formats depending on what the user asks for. But if I have to choose, I would say XML and almost never JSON (XML is just an example, we use binary format). I went the second way you suggested - configuring custom converters instead of the default ones.
- I have implemented custom
MessageConverter
extendingAbstractMessageConverter
. In the constructor I've registered appropriate supported MimeType. - Then I've registered my custom converter by overriding
WebSocketMessageBrokerConfigurer
'sconfigureMessageConverters
, and returnfalse
from the method to not add default converters. - As soon as my controller returns the value I get NPE in
SendToMethodReturnValueHandler
'spostProcessMessage
. This happens becauseCompositeMessageConverter
contains only a single converter - my custom one. But my converter failsAbstractMessageConverter
'ssupportsMimeType
check andAbstractMessageConverter
'stoMessage
returnsnull
. Thisnull
causes the exception inpostProcessMessage
.
As the workaround I can register additional default MimeType application/json
for my custom converter. But it looks like too dirty to me.