Spring MVC: is it possible to return a map <String, Object> dynamically converted to JSON using Jackson?
I want to enable my Spring MVC web application to return the state of the model represented as JSON.
I understand that by annotating the @ResponseBody controller method, you can convert between JSON and objects of the appropriate type. However, the model object that I want to view speaks directly in the database, not supporting the state itself.
So I wonder, can I just fill out a map (like a HashMap) and make serialized Jackson instead? I understand that I could create new View classes for my stateful models, but I would prefer not to.
Thanks.
I am returning a Map<String, ?> From several of my controllers, and the content is automatically converted to JSON by Jackson - as you say, it is easier to do it this way when you do not yet have a domain object that can hold the information you want to return.
This should be done automatically for you if you have jackson libraries in your classpath and <mvc:annotation-driven/> in the spring configuration. The maven dependencies that I use for Jackson are:
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.8.5</version> <scope>runtime</scope> </dependency>