I recently had the need to write a RESTful web service. Spring has re-engineered the MVC project to support REST based services (because the large overlap in annotation support), whereas it has left Spring Web Services as it’s own project with a distinct life cycle.
In Spring MVC, there are two ways to render a response, one using the conventional ViewResolver interface and the other using the HttpMessageConverter interface. I was a bit cloudy on when to use which, when I stumbled on this post from Keith Donald which brings out the difference succinctly:
Views vs. @ResponseBody (aka HttpMessageConverters)
Two different systems exist for rendering responses
• ViewResolver + View
• HttpMessageConverter
Triggered in different ways
• Render a view by returning a String
• Write a message by returning a @ResponseBody Object or ResponseEntity
Which one do I use?
• Use views to generate documents for display in a web browser
• HTML, PDF, etc
• Use @ResponseBody to exchange data with web service clients
• JSON, XML, etc
That cleared up things nicely for me and I hope it helps others too!